2016-03-05 36 views
0

我在看MSDN网络,任何人都可以解释我的source-codeObject.create源码解释?

Object.create = (function() { 
    var Temp = function() {}; 
    return function (prototype) { 
     if (arguments.length > 1) { 
     throw Error('Second argument not supported'); 
     } 
     if (typeof prototype != 'object') { 
     throw TypeError('Argument must be an object'); 
     } 
     Temp.prototype = prototype; 
     var result = new Temp(); 
     Temp.prototype = null; 
     return result; 
    }; 
    })(); 

function Guru(name){ 
    this.name = name; 
} 


function Shankar(name){ 
    this.name = name; 
} 

Guru.prototype = Object.create(Shankar.prototype); 

这到底是怎么困惑我是Temp.prototype = null;,为什么我们把它设置为nullreturning a instance of Temp时,我们可以只返回new Temp

Temp.prototype = prototype; 
return new Temp; 

回答

1

也许只是让它不缓存的最后一个对象,如果原始对象被删除,请将其保存在内存中。在绝大多数情况下似乎没有必要,但对审慎而言不是一个坏主意。