闭包中的实例

 1 function Universe() {
 2         //缓存实例
 3         var instance = this;
 4         
 5         // 重写构造函数
 6         Universe = function(){
 7             return instance;
 8         };
 9 
10         this.start_time = 0;
11         this.bang = "Big";
12 
13         //保留原型属性
14         Universe.prototype = this;    //var a = {}, b = {};  a = b; a.no = "no";  b.no;// "no"
15                                     //对象的引用,这里若定义Universe.prototype 就是 定义 this 的属性   证据A
16 
17         //实例
18         instance = new Universe();
19 
20         //重置构造函数指针
21         instance.constructor = Universe;
22 
23         //所有功能
24         instance.start_time = 0;
25         instance.bang = "Big";
26 
27         return instance;
28     }
29 
30     Universe.prototype.nothing == true;
31     
32     var uni = new Universe();
33 
34     Universe.prototype.everything = true;
35     var uni2 = new Universe();
36     
37 
38     console.log(uni === uni2);                            //true
39 
40     console.log(uni.constructor === Universe);            //true
41 
42     console.log(Universe.prototype);                    //其实就是 Universe 对象   证据A
43 
44     console.log(uni.everything);                        //true        证据A

 

posted @ 2013-09-02 17:44  楚玉  阅读(266)  评论(0编辑  收藏  举报