javascript prototype&_proto_&constructor

1,每一個對象都有一個__proto__屬性。html

2,每一個構造函數都有一個prototype的方法。數組

3,_proto_指向自身構造函數的prototype方法。函數

4,constructor 返回建立該對象的構造函數。this

注:(構造函數標準爲大寫開頭,如Function(),Object()等等JS中自帶的構造函數,以及本身建立的)都具備一個名爲prototype的方法,js中的函數也是對象。因此函數也帶有__proto__屬性。spa

 

 1      // 構造函數
 2     function Foo(y) {
 3         // 構造函數將會以特定模式建立對象:被建立的對象都會有"y"屬性
 4         this.y = y;
 5     }
 6     // "Foo.prototype"存放了新建對象的原型引用
 7     // 因此咱們能夠將之用於定義繼承和共享屬性或方法
 8     // 因此,和上例同樣,咱們有了以下代碼:
 9     // 繼承屬性"x"
10     Foo.prototype.x = 250;
11     // 繼承方法"calculate"
12     Foo.prototype.calculate = function(z) {
13         return this.x + this.y + z;
14     };
15     // 使用foo模式建立 "b" and "c"
16     var b = new Foo();
17     var c = new Foo();
18     // 調用繼承的方法
19     b.calculate(); // 
20     c.calculate(); // 
21     // 讓咱們看看是否使用了預期的屬性
22     console.log(
23         b.__proto__ === Foo.prototype, // true
24         c.__proto__ === Foo.prototype, // true
25         // "Foo.prototype"自動建立了一個特殊的屬性"constructor"
26         // 指向a的構造函數自己
27         // 實例"b"和"c"能夠經過受權找到它並用以檢測本身的構造函數
28         b.constructor === Foo, // true
29         c.constructor === Foo, // true
30         Foo.prototype.constructor === Foo, // true
31         b.calculate === b.__proto__.calculate, // true
32         b.__proto__.calculate === Foo.prototype.calculate // true
33     );
34 
35 
36     // 字符串:String()
37     var str = "張三";
38     console.log(str.constructor); // function String() { [native code] }
39     console.log(str.constructor === String); // true
40     // 數組:Array()
41     var arr = [1, 2, 3];
42     console.log(arr.constructor); // function Array() { [native code] }
43     console.log(arr.constructor === Array); // true
44     // 數字:Number()
45     var num = 5;
46     console.log(num.constructor); // function Number() { [native code] }
47     console.log(num.constructor === Number); // true
48     // 自定義對象:Person()
49     function Person() {
50         this.name = "CodePlayer";
51     }
52     var p = new Person();
53     console.log(p.constructor); // function Person(){ this.name = "CodePlayer"; }
54     console.log(p.constructor === Person); // true
55     // JSON對象:Object()
56     var o = {
57         "name": "張三"
58     };
59     console.log(o.constructor); // function Object() { [native code] }
60     console.log(o.constructor === Object); // true
61     // 自定義函數:Function()
62     function foo() {
63         alert("CodePlayer");
64     }
65     console.log(foo.constructor); // function Function() { [native code] }
66     console.log(foo.constructor === Function); // true
67     // 函數的原型:bar()
68     function bar() {
69         alert("CodePlayer");
70     }
71     console.log(bar.prototype.constructor); // function bar(){ alert("CodePlayer"); }
72     console.log(bar.prototype.constructor === bar); // true

 

 1 function Fun(){
 2             }
 3 //            我創造了一個函數Fn
 4 //            這個函數由Function生成(Function做爲構造函數)
 5             var fn=new Fun()
 6 //            我建立了一個函數fn
 7 //            這個函數由Fn生成(Fn做爲構造函數)
 8             
 9             
10             console.log(fn.__proto__===Fun.prototype)    //true
11 //            fn的__proto__指向其構造函數Fun的prototype
12             console.log(Fun.__proto__===Function.prototype)        //true
13 //            Fun的__proto__指向其構造函數Function的prototype
14             console.log(Function.__proto__===Function.prototype)    //true
15 //            Function的__proto__指向其構造函數Function的prototype
16 //            構造函數自身是一個函數,他是被自身構造的
17             console.log(Function.prototype.__proto__===Object.prototype)    //true
18 //            Function.prototype的__proto__指向其構造函數Object的prototype
19 //            Function.prototype是一個對象,一樣是一個方法,方法是函數,因此它必須有本身的構造函數也就是Object
20             console.log(Fun.prototype.__proto__===Object.prototype)         //true
21 //             與上條相同
22 //             此處能夠知道一點,全部構造函數的的prototype方法的__都指向__Object.prototype(除了....Object.prototype自身)
23             console.log(Object.__proto__===Function.prototype)        //true
24 //            Object做爲一個構造函數(是一個函數對象!!函數對象!!),因此他的__proto__指向Function.prototype
25             console.log(Object.prototype.__proto__===null)        //true
26 //            Object.prototype做爲一切的源頭,他的__proto__是null
27 
28 //            下面是一個新的,額外的例子
29 
30             var obj={}
31 //            建立了一個obj
32             console.log(obj.__proto__===Object.prototype)        //true
33 //            obj做爲一個直接以字面量建立的對象,因此obj__proto__直接指向了Object.prototype,而不須要通過Function了!!
34 
35 //            下面是根據原型鏈延伸的內容
36 //            還有一個上文並未提到的constructor,  constructor在原型鏈中,是做爲對象prototypr的一個屬性存在的,它指向構造函數(因爲主要講原型鏈,這個就沒在乎、);
37 
38             console.log(obj.__proto__.__proto__===null)        //true
39             console.log(obj.__proto__.constructor===Object)        //true
40             console.log(obj.__proto__.constructor.__proto__===Function.prototype)        //true
41             console.log(obj.__proto__.constructor.__proto__.__proto__===Object.prototype)    //true    
42             console.log(obj.__proto__.constructor.__proto__.__proto__.__proto__===null)        //true
43             console.log(obj.__proto__.constructor.__proto__.__proto__.constructor.__proto__===Function.prototype)    //true
44             

 

代碼轉自兩位大神的例子。謝謝了。此處存儲.net

http://www.cnblogs.com/libin-1/p/6014925.htmlprototype

http://www.jb51.net/article/74394.htmcode

相關文章
相關標籤/搜索