Training Prototype

Edit: For those people who see this post in the future, this site has undoubtedly been critical for me to digest Javascript. If you come from the traditional OOP background, I highly recommend it. UML-esq diagrams were awesome.

I still can't figure out what the .prototype property is in Javascript. Is this just a link to another object? Or is it a link to a pointer to another object? I came from C / C ++ / x86 and just can't see how this works. Let's look at some examples of how I see things now; this will help point out my mistakes to see how everything works. I don’t even know if some of them are valid syntax. Object and Function are global objects / objects respectively.

 1 // Global.prototype = ?? 2 // Function.prototype = ?? 3 4 var obj1 = {}; // obj1.prototype = Object 5 obj2 = {}; // obj2.prototype = Object 6 7 var func1 = function() {}; // func1.prototype = Function 8 func2 = function() {}; // func2.prototype = Function 9 function func3() {} // func3.prototype = Function 10 

I'm so confused.

 11 var Foo = function() { this.prop1 = 0; } 12 var foo = new Foo(); // should it be 'new Foo' or 'new Foo()'? 13 // Foo.prototype = Function 14 // foo.prototype = Foo 15 var Goo = function() { this.prop2 = 0; } 16 var goo = new Goo(); 17 // goo.prototype = Goo 18 goo.prototype = new Foo(); 19 // goo.prop1 now exists ? 

I also don't understand how to change prototypes around.

 20 function A () { 21 this.prop1 = 1; 22 } 23 function B () { 24 this.prop2 = 2; 25 } 26 function C () { 27 this.prop3 = 3; 28 } 29 C.prototype = new B(); 30 var c = new C(); 31 // c.prop1 = 1 32 // c.prop2 = 2 33 // c.prop3 = undefined 34 C.prototype = new A(); 35 // c.prop2 = 2??? 36 // c.prop3 = 3 

I can not understand the concept. I don’t quite understand. I don’t understand how the cloned objects get their local copies of the data, but changes to the original object (prototype) are somehow cascaded to the clones. I was messing around with FigureBug trying to figure it out, but mentally I can't come up with an idea that is consistent with every ive example seen

C ++ can be a huge monster, but at least I know exactly what is going on. Here ... I use my best guess .. I guess this is just a new paradigm. Anyway, thanks, if you can help ... I turned up on this .prototype.

+8
javascript
source share
2 answers

Wow, this is a lot of questions. Let it work through them.

Is this just a link to another object? Or is it a link to a pointer to another object?

JavaScript has no pointers. However, a variable or property containing an β€œobject” actually contains a reference to the object, so other variables may contain references to the same object.

 Global.prototype = ?? 

The global object ( window , if you want), where all global variables are defined, does not have a prototype (do not care about exceptions in some environments).

I still can't figure out what the .prototype property is in Javascript.

The .prototype property is a property that all function objects have, pointing to their prototype object (a simple object).

You should not confuse it with the prototype internal link that every object has. This prototype points to an object that looks at properties that the object does not have.

 Function.prototype = ?? 

This is the object from which all Function objects are inherited. It contains things like call or bind .

 var obj1 = {}; // obj1.prototype = Object var func1 = function() {}; // func1.prototype = Function 

Yes, kinda. I think you have a concept, but I do not know the terminology. While func.prototype is different, the obj.prototype property does not even exist. However, you meant internal prototypes - we can access them through the Object.getPrototypeOf function. And they do not relate to constructor functions, but relate to their prototype objects. Here's the fix:

 Object.getPrototypeOf(obj1) === Object.prototype Object.getPrototypeOf(func1) === Function.prototype 

should it be "new foo" or "new foo ()"?

It does not matter, they are equivalent. You only need parentheses when you want to pass arguments.

 var Foo = function() { this.prop1 = 0; } var foo = new Foo(); 

Again, your assumptions were correct, but were expressed incorrectly. Let me work with the details.

Above, I talked about "prototype objects." What is it? These are simple objects that are implicitly created with each function. In this case, it is Foo.prototype - an empty object. So, we are dealing with three objects: the Foo constructor, its prototype Foo.prototype and an instance of Foo .

What is special about Foo ? This is the first thing the new operator does. When a function is called as a constructor - using new - then its .prototype property is .prototype and a new object is created with its internal prototype set for this prototype object. This is a miracle. After that, the function is called in a new instance, so this is a new object; and in your case, it creates the .prop1 property on the instance. Then the result is returned (and assigned to Foo ).

How to use this magic now? The point is to create the properties of a prototype object, which will then be inherited.

 // modify `Foo.prototype`, the object which `foo` inherits from: Foo.prototype.prop2 = 1; console.log(foo.prop2); // 1! 

I also don't understand how to change prototypes around.

The problem is that this is completely impossible. After creating the instance, the prototype chain of the object is pretty static. However, you do not need this often.

 goo.prototype = new Foo(); 

This did not work because the .prototype property .prototype not what you expected.

 var c = new C(); C.prototype = new A(); 

It only works a little. See above what new does - it searches for the constructor's .prototype property only once. The internal prototype remains locked - you do not change the instance c . However, if you now create a new instance of var d = new C() , it will inherit instance A

I hope this helps; If you have additional questions, please comment.

+4
source share

The prototype is used to search for properties when the object itself does not define it while reading. Records always happen on the object itself

When you call

 // An instance of Foo ({prop1:0}) is set as the // prototype (to all instances of Goo) var goo = new Goo(); // 0, from the prototype console.log(goo.prop1); // Read from the object itself (set in Goo constructor) console.log(goo.prop2); // New instance, same prototype as goo var goo2 = new Goo(); console.log(goo2.prop1) ; // 0, still from the prototype // Setting is always at the object level, not the prototype goo.prop1 = 5; // goo2 is still reading from the prototype console.log(goo2.prop1); 

NB

Do not instantiate the parent class just to set up inheritance. See why here http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html

+2
source share

All Articles