I have read my javascript since childhood and after that I should override the manufacturer in case of prototype heritage. But I am surprised to see that the following example gives the same output to the console, even if the override controller statement is commented on. Please enlighten.
function A () {console.info ("A Creator"); } Function B () {console.info ("b constructor"); } B. Prototype = estrototype; B.prototype.constructor = B; // Do we really need this statement? Console.info ("CP1"); Var B = new B (); Console.info ("CP2"); Before participating the Constructor, there is a problem in your code. You Prototypeandare doing both by applying aprototype,bipartotype = aprototype;This means that you can not effectively identify the parents of objects created by these functions. Try it
console.log (New B) Example A, New B (Example B); // True TruthThis is expected, because
Bis created byBandBIs made fromaprototype butconsole.log (new A () example a, new A () example b); // True TruthWhaat? An example of
ais an example of howbis? Because since you haveb. As prototypeis made, in the form of aas a prototype, when an objectAis its prototype (aprototypeA < The prototype of the object created with / code> is that the prototype oftoBexists anywhere in the series, becauseB.prototypeis the same asaprototype, < Code> AB. .The correct way to do this is,
B. Prototype = object. (Aprototype );Now, you are creating a
Now, if we do not do it
<constructor = b;
constructorproperty of any object created byBwithout that code Try printingConsole.log ((new b ()). Constructor); // [function: A]Since the prototype of
bstillAtoprototype, It still refers to the functionA. This is why we change it with theBfunction object.
No comments:
Post a Comment