2015-12-16 28 views
1

JavaScript Object.create()的polyfill,由我完全困惑的代码。代码链接是polyfill of Object createjavascript的polyfill用简单的形式创建对象

if (typeof Object.create != 'function') { 
 
    // Production steps of ECMA-262, Edition 5, 15.2.3.5 
 
    // Reference: http://es5.github.io/#x15.2.3.5 
 
    Object.create = (function() { 
 
     // To save on memory, use a shared constructor 
 
     function Temp() {} 
 

 
     // make a safe reference to Object.prototype.hasOwnProperty 
 
     var hasOwn = Object.prototype.hasOwnProperty; 
 

 
     return function(O) { 
 
      // 1. If Type(O) is not Object or Null throw a TypeError exception. 
 
      if (typeof O != 'object') { 
 
       throw TypeError('Object prototype may only be an Object or null'); 
 
      } 
 

 
      // 2. Let obj be the result of creating a new object as if by the 
 
      // expression new Object() where Object is the standard built-in 
 
      // constructor with that name 
 
      // 3. Set the [[Prototype]] internal property of obj to O. 
 
      Temp.prototype = O; 
 
      var obj = new Temp(); 
 
      Temp.prototype = null; // Let's not keep a stray reference to O... 
 

 
      // 4. If the argument Properties is present and not undefined, add 
 
      // own properties to obj as if by calling the standard built-in 
 
      // function Object.defineProperties with arguments obj and 
 
      // Properties. 
 
      if (arguments.length > 1) { 
 
       // Object.defineProperties does ToObject on its first argument. 
 
       var Properties = Object(arguments[1]); 
 
       for (var prop in Properties) { 
 
        if (hasOwn.call(Properties, prop)) { 
 
         obj[prop] = Properties[prop]; 
 
        } 
 
       } 
 
      } 
 

 
      // 5. Return obj 
 
      return obj; 
 
     }; 
 
    })(); 
 
}

  1. 为什么它通过使用IIFE逻辑复杂(立即调用功能Express)和返回功能,并关闭。
  2. 相反,我可以使用下面的简单逻辑和代码吗?内容中是否有错误或不恰当的内容?没有IIFE和返回功能。

if (typeof Object.createOwn != "function") { 
 
    Object.createOwn = function(O) { 
 
     // 1. if Type(O) is not Object or Null throw a TypeError exception. 
 
     if (typeof(O) != "object") { 
 
      throw TypeError("Object prototype may only be an Object or null"); 
 
     } 
 

 
     // 2. Let obj be the result of creating a new object as if by the 
 
     // expression new Object() where Object is the standard built-in 
 
     // constructor with that name 
 
     // 3. Set the [[Prototype]] internal property of obj to O. 
 
     var obj; 
 
     var Temp = function() {}; 
 
     Temp.prototype = O; 
 
     obj = new Temp(); 
 

 
     // 4. If the argument Properties is present and not undefined, add 
 
     // own properties to obj as if by calling the standard built-in 
 
     // function Object.defineProperties with arguments obj and Properties 
 
     if (arguments.length > 1) { 
 
      var Properties = Object(arguments[1]); 
 
      for (var prop in Properties) { 
 
       if (Properties.hasOwnProperty(prop)) { 
 
        obj[prop] = Properties[prop]; 
 
       } 
 
      } 
 
     } 
 
     return obj; 
 
    } 
 
} 
 

 
var foo = { 
 
    one: 1, 
 
    two: 2 
 
}; 
 

 
var bar = Object.createOwn(foo, 3);

回答

1

他们都将工作,但原来是使用IIFE的几个原因。其中两人在评论

// To save on memory, use a shared constructor 

提到这不是您的版本,其中var Temp = function() {};被裹入功能和新的实例被创建每次使用它时的情况。

// make a safe reference to Object.prototype.hasOwnProperty 

由于Object.prototype.hasOwnProperty可能被重写为当它的时候使用它,将填充工具可以确保它有它自己的每Object.create安全引用。

然后这也是许多使用IIFE的常见原因,以避免污染全局名称空间。

这些主要是保障措施,在这种情况下不需要。但我认为没有理由将其删除。