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 dont understand:
At the first log the foo.prototype had the __proto__ attribute which was an Object
At the second log the test.__proto__ had the __proto__ attribute which was an Object
When to use __proto__ and when prototype, what is the difference?
UPDATE:
In John Resig’s blog in the instanceOf section there is something waht 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 is true
Why is the Object.getPrototypeOf(asd) throws error? This is a string, isn’t it?
Always use
prototypeorObject.getPrototypeOf.__proto__is non-standard and has been deprecated by Mozilla.John Resig has a good blog entry about it.
The reason why
test.prototypeis undefined is because you created a new object that does not have a constructor prototype. Here’s an example of when to useObject.getPrototypeOf.As you can tell, inheritance in JavaScript is tricky. Lets look at an example:
The string literal is of type string. The same is true when calling
String()as a function. Now, because of behavior of thenewoperator, a new object is created, with the prototype ofString()as its parent.Because JS likes to coerce things, you can get at the string literal a few ways:
Prototypal Inheritance in JavaScript by Crockford helped me a lot when learning about JS inheritance.
From Mozilla’s Strings page: