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.
source share