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

polyfill of javascript Object create with simple form

javascript Object.create() 的 polyfill,由一些我完全混淆的代码。代码 link 是 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(立即调用函数表达式)和 return 函数以及闭包会使逻辑复杂化。
  2. 相反,我可以使用下面的简单逻辑和代码吗?是否有任何错误或不适当的内容?没有 IIFE 和 return 函数。

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);

它们都可以工作,但出于某些原因,原始版本使用 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.

都有自己的安全引用

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

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