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 Prototype
andare 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 Truth
This is expected, because
B
is created byB
andB
Is made froma
prototype butconsole.log (new A () example a, new A () example b); // True Truth
Whaat? An example of
a
is an example of howb
is? Because since you haveb. As prototype
is made, in the form of aas a prototype
, when an objectA
is its prototype (aprototype
A < The prototype of the object created with / code> is that the prototype of
toB
exists anywhere in the series, becauseB.prototype
is 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;
constructor
property of any object created byB
without that code Try printingConsole.log ((new b ()). Constructor); // [function: A]
Since the prototype of
b
stillA
toprototype
, It still refers to the functionA
. This is why we change it with theB
function object.
No comments:
Post a Comment