8
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??

Geek Num 88
  • 5,264
  • 2
  • 22
  • 35
Mohammad Faizan khan
  • 1,213
  • 3
  • 17
  • 32
  • Own properties have preference, that's how the prototype chain works. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain – elclanrs Feb 27 '14 at 06:38

3 Answers3

3

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

Community
  • 1
  • 1
Stephen Kaiser
  • 1,545
  • 14
  • 10
  • the only adventage is to save memoray oki but how can i manipulate the constructor values or func – Mohammad Faizan khan Feb 27 '14 at 06:55
  • 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 Feb 27 '14 at 07:03
  • You could also basically just change the value directly outside of any function: `ninja.shouldSwingSword = false;`. Is this what you are asking? – Stephen Kaiser Feb 27 '14 at 07:12
1

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
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
Andrew Templeton
  • 1,656
  • 10
  • 13
-1

A simple example of Object.create:

const obj = {
    hello: function() { return this.name },
  name: "karl"
}

const bla = Object.create(obj);
const wha = Object.create(obj);

console.log(obj.hello()); // karl
console.log(bla.hello()); // karl
console.log(wha.hello()); // karl

wha.name = "derp"

console.log(obj.hello()); // karl
console.log(bla.hello()); // karl
console.log(wha.hello()); // derp

obj.name = "hihi"

console.log(obj.hello()); // hihi
console.log(bla.hello()); // hihi
console.log(wha.hello()); // derp
basickarl
  • 37,187
  • 64
  • 214
  • 335