JavaScript: property assignment through prototype -
I am struggling to understand the difference in the following 2 set codes from the original code and I have a bit simpler for myself Made it.
Question: I think I understand how CodeA works Ninja.prototype.swung = false
function ninja () Specifying a new property in
, and ninjiaA.swung
because it makes a false evaluation. However, in CodeB, when we declare the function with ninja ()
with this.swung = true
, then Ninja.prototype.swung = false
takes no effect, and ninja. Swung
remains on the right to evaluate. I have not been able to understand why it does not work later in the assignment codebase. Can anyone enlighten me on this?
Code:
Function Ninja () {} Ninja.prototype.swung = false; Var ninja = new ninja (); NinjaA.swung; // Valuation for false
CodeB:
function ninja () {this.swung = true; } Ninja.prototype.swung = false; // I'm expecting this change lies, // but it is not. Var ninja = new ninja (); NinjaA.swung; // evaluates the truth
Thank you very much ago.
when you have a property using the this
inside the constructor function Whenever you declare an asset on the prototype of that constructor function, then it remains there and see all objects of that constructor. When you have the same in the prototype series in the object Name , then the property's property hides one on the prototype.
Think how the asset is evaluated. The prototype series which can explain things.
Code:
ninja. Swung 1. The property of the current object is inclined - not 2. Swing is a property of prototype of the current object - yes 2.1. Return it to
CodeB:
In Code B, it never goes on the property on prototype.
Comments
Post a Comment