overriding prototype property or function

13,530

Solution 1

The reason to define a function on the prototype is so that it is shared between all instances. This will save you some memory rather than each instance having its own copy of a function defined in the constructor.

Some other references you might be interested in:

Javascript when to use prototypes

http://javascript.crockford.com/inheritance.html

Solution 2

This is by design. Do not set the value in the constructor if you want it to return false.

You can also make a setter method:

function Ninja() {
    var swordState = true;
    this.swingSword = function () {
        return swordState;
    };
    this.setSword = function (b) {
        swordState = b;
    };
}

// Should return false, but will be overridden
Ninja.prototype.swingSword = function () {
    return false;
};

var ninja = new Ninja();
console.log(ninja.swingSword(), "Calling the instance method, not the prototype method.");
ninja.setSword(false);
console.log(ninja.swingSword()); // returns false
Share:
13,530
Mohammad Faizan khan
Author by

Mohammad Faizan khan

Graduate of SZIC, University of Karachi. I am a programmer, researcher and Instructor. Love to talk about Programming, Information Systems and Databases. Here, on stackOverFlow to Solve my problems:) and secondly (not importantly) to solve other problems!

Updated on June 19, 2022

Comments

  • Mohammad Faizan khan
    Mohammad Faizan khan about 2 years
    function Ninja(){
      this.swingSword = function(){
        return true;
      };
    }
    
    // Should return false, but will be overridden
    Ninja.prototype.swingSword = function(){
      return false;
    };
    
    var ninja = new Ninja();
    log( ninja.swingSword(), "Calling the instance method, not the prototype method." );
    

    now log showing me true. which means swingSword that were defined in Ninja.prototype has overridden so how can i override the constructor function or property.?? i know that preference is given to constructor variable then why need to define a function or property inside prototype??

  • Mohammad Faizan khan
    Mohammad Faizan khan over 10 years
    the only adventage is to save memoray oki but how can i manipulate the constructor values or func
  • Stephen Kaiser
    Stephen Kaiser over 10 years
    To change an instance variable value that you set in the constructor, you can reference it using this. So, if you had set something like this.shouldSwingSword = true; in the constructor, you could set this.shouldSwingSword = false; in your function. Then, you do checks like if ninja.shouldSwingSword. You should probably not need to put functions inside the constructor, though. Those should typically go on the prototype or just the class object itself (i.e., a static method)
  • Stephen Kaiser
    Stephen Kaiser over 10 years
    You could also basically just change the value directly outside of any function: ninja.shouldSwingSword = false;. Is this what you are asking?