javascript的polyfill用简单的表单创建对象

polyfill of javascript Object create with simple form

本文关键字:表单 创建对象 简单 polyfill javascript      更新时间:2023-09-26

javascript Object.create()的polyfill,被一些代码弄糊涂了。代码链接是Object create的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(立即调用函数表达式)和返回函数以及闭包会使逻辑复杂化
  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);

它们都能工作,但由于一些原因,最初的版本使用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的常见原因,以避免污染全局命名空间。

这些大多是保障措施,在这种情况下不需要。但我看不出有什么理由删除它们。