javascript对象的polyfill用简单的形式创建

时间:2015-12-16 18:25:27

标签: javascript closures polyfills iife

javascript Object.create()的polyfill,由我完全困惑的一些代码。代码链接为polyfill of Object create

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(Immediately Invoked Function Express)和返回函数以及闭包使逻辑变得复杂。
  2. 相反,我可以使用下面的简单逻辑和代码吗?是否有任何错误或不适当的内容?没有IIFE和返回功能。
  3. 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 个答案:

答案 0 :(得分:1)

他们都会工作,但原来使用IIFE有几个原因。评论中提到了其中两个

// To save on memory, use a shared constructor

您的版本不是这种情况,var Temp = function() {};被包装到函数中,每次使用时都会创建一个新实例。

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

由于Object.prototype.hasOwnProperty在使用时可能会被覆盖,因此polyfill会确保它在每个Object.create都拥有它的安全引用。< / p>

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

这些主要是安全措施,在这种情况下不需要。但我认为没有理由将它们删除。