javascript的polyfill用简单的表单创建对象
polyfill of javascript Object create with simple form
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;
};
})();
}
- 为什么使用IIFE(立即调用函数表达式)和返回函数以及闭包会使逻辑复杂化
- 相反,我可以使用下面的简单逻辑和代码吗?里面有什么错误或不恰当的内容吗?没有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的常见原因,以避免污染全局命名空间。
这些大多是保障措施,在这种情况下不需要。但我看不出有什么理由删除它们。
相关文章:
- 使用Web Html表单创建Javascript数组
- 为网站表单创建一个专业的日历
- 如何从表单变量对象中获取特定的from属性
- 从包含表单的对象中提取值
- 如何使用rails 3.1.4中的FormBuilder.new构建表单生成器对象
- 如何在 Dreamweaver 上为我的表单创建摘要页面
- 如何获取每个元素作为键传递给帖子表单的对象
- jQuery Mobile:动态表单创建显示本机控件与jQuery Mobile插件控件
- 如何为注册表单创建圆形轮廓仪表
- 我可以使用Orbeon表单创建具有固定位置的HTML表单字段吗
- javascript的polyfill用简单的表单创建对象
- OOP中的动态表单创建和提交
- 为动态表单创建处理程序
- 提交时如何使用html表单创建页面或html代码
- 使用javascript通过表单发送对象
- 在aspx中为应用程序表单创建选项卡
- 从Web表单创建代码片段(易于剪切和粘贴)
- 如何通过输入表单将对象数组添加到html列表
- Javascript和动态表单创建
- Javascript表单验证对象