Javascript prototype and __proto__ and getPrototypeOf problem

I have a simple class in javascript:

function foo() { this.bar = "bar"; } var test = new foo; console.log(foo.prototype,foo.__proto__) /*output: foo { constructor: function foo() { __proto__: Object } , function Empty() {} */ console.log(test,test.prototype,test.__proto__,test.__proto__.__proto__) /*output: foo { bar: "bar" __proto__: foo } , undefined , foo { constructor: function foo() { __proto__: Object } , Object { ... } */ 

What? I do not understand:

In the first log, foo.prototype had the __proto__ attribute, which was Object In the second test.__proto__ had the __proto__ attribute, which was Object

When to use __proto__ and when prototype , what is the difference?


UPDATE:

There is something in the John Resig blog in the instanceOf section I don’t understand:

If I use var asd = "asd"; Object.getPrototypeOf (asd) , it throws a TypeError , but
If I use var dsa = new String("dsa"); Object.getPrototypeOf (dsa) , it returns String
But asd.constructor.prototype == dsa.constructor.prototype true

Why Object.getPrototypeOf (asd) Object.getPrototypeOf error? This is a string, right?

+4
source share
2 answers

Always use prototype or Object.getPrototypeOf .

__proto__ is non-standard and deprecated by Mozilla .

John Resig has a good blog entry about this.


The reason test.prototype is undefined is because you created a new object that does not have a prototype constructor. Here is an example using Object.getPrototypeOf .

 js> function foo(){} js> typeof foo.prototype; object js> var f = new foo(); js> typeof f.prototype; undefined js> typeof Object.isPrototypeOf(f); object js> typeof f.constructor.prototype; object js> foo.prototype === Object.getPrototypeOf(f); true js> foo.prototype === f.constructor.prototype; true 

As you can tell, JavaScript inheritance is complicated. Let's look at an example:

 js> typeof "asdf"; string js> typeof String("asdf"); string 

A string literal has a type string. The same is true when calling String() as a function. Now, due to the behavior of the new operator, a new object is created with the String() prototype as its parent.

 js> var s = new String("asdf"); js> typeof s; object 

Since JS likes to force things, you can get a string literal in several ways:

 js> s asdf js> s.valueOf(); asdf js> typeof s object js> typeof s.valueOf(); string 

Crockford's Prototypal Inheritance in JavaScript helped me a lot by learning about JS inheritance.

From Mozilla Lines :

String objects can be created by calling the new String () constructor. A String object wraps a JavaScript String primitive data type with the methods described below. Global, you can also call the String () function without a new one before creating a primitive string. JavaScript string literals are primitive strings.

+6
source

__proto__ is an intermediate object between the object and its prototype. The biggest advantage to using it is that you can completely change the prototype chain for an object without changing the instance or prototype.

Example:

 function F() {} F.prototype.a = 1; var f = new F(); f.__proto__ = { b : 2 }; "a" in f => false; "b" in f => true; 

But, as Jeremy said, __proto__ deprecated. The only use case would be if you wanted to add a set of properties to an object, changing its prototype without having to repeat each of them. Not really.

+5
source

All Articles