3

I want to override a member function in constructor function not the whole constructor function but only one member function inside it, my constructor function look like

function viewModel(){
  var self= this;
  self = {
          testFuncA:function(){
            console.log("you are in not overrided function test FuncA");
          },
          testFuncB:function(){
            console.log("you are in not orverided function test FuncB");
          }
      };
  }

and in override.js i want to override it like this

viewModel.prototype.testFuncA = function(){
      console.log("you are in overrided function test funcA");  
 }

and when i create object of viewModel constructor function

var obj = new viewModel();
obj.testFuncA();

then output should be you are in overrided function test funcA, but it print you are in not overrided function test FuncA which is not my desire output how may i achieve desire output.

When only core.js file is loaded in my web page then the testFuncA will output as you are in not overrided function test FuncA but when i load both files, that is core.js and after that override.js then testFuncA will output you are in overrided function test funcA.

2
  • 1
    self in this piece of code is local to viewModel scope, you can't access them in an outer scope Commented Nov 16, 2015 at 8:52
  • Maybe we miss some important part of your code. As shared, the self = this is overridden by self = { /* … */}, so self becomes totally private as mentioned by Hacketo. Only objects defined within that constructor will be able to use self. Commented Nov 16, 2015 at 9:05

3 Answers 3

2

So first off, we cannot change the inner workings of this constructor function, you can only do that sort of thing with a prototype, so instead you will need to completely override it in the namespace. In override.js simply have the same code like this:

function viewModel(){
    this.testFuncA = function(){
        console.log("This is an overrided function test FuncA");
    };
    this.testFuncB = function(){
        console.log("This is an function test FuncB");
    };
}

So this will replace your original function in the namespace, ultimately achieving your goal. Had you been using a prototype system where you copy from an existing object, you'd simply have to do something along the lines of Proto.testFuncA = function(){...}

Note: I changed the code as a suggestion on how to handle your constructors, as a constructor is passed a new object via 'this' already, no need to make another. You also had 'Self' as a reference to 'this' but then by making a new object you actually only changed the reference in Self to the new object, you did not actually alter this in any way. instead you altered this new object that 'Self' was pointing to.

Note2: So if you would like to use prototypes. I would read through this Why is it necessary to set the prototype constructor? for a better understanding of prototypes. And this as well http://www.w3schools.com/js/js_object_prototypes.asp

Sign up to request clarification or add additional context in comments.

Comments

1

In core.js if you have something like this

      function viewModel(){
      }
      viewModel.prototype.testFuncA = function(){
        console.log("you are in not overrided function test FuncA");
      };
      viewModel.prototype.testFuncB = function(){
        console.log("you are in not orverided function test FuncB");
      };

Then in override.js you can have soem thig like this

      viewModel.prototype.testFuncA = function(){
            console.log("you are in overrided function test funcA");  
       }

Edited :-- in case you dont want to make changes in core.js

var viewModelParent = viewModel;
var myViewModel = new viewModelParent();

var viewModel = function(){
    this.testFuncA = function(){
            console.log("you are in overrided function test funcA");  
    }
};
viewModel.prototype = myViewModel;

6 Comments

Note that you put the declerations for the prototypes inside of the constructor. These should only be declared once outside of the constructor.
there are hundreds of functions in viewModel constructor function which i cant convert to like that you defined needs some generic solution which can fit in my given scenario.
@Sja91 its not inside , have a look again, its empty constructor
@MansoorAkhtar do you have access to core.js? you dont want to make changes in core.js? can you give more detail?
@LNT it have updated my question you can check it now.
|
0

core.js can do some change like

function viewModel(){

}
viewModel.prototype = {
      testFuncA:function(){
        console.log("you are in not overrided function test FuncA");
      },
      testFuncB:function(){
        console.log("you are in not orverided function test FuncB");
      }
}

in override.js use code below

viewModel.prototype.testFuncA=function(){
     //TODO::
}

U should confirm core.js load before override.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.