如何在ECMAScript 5之前完成第二个参数的Object.create()? [英] How was Object.create() with second parameter accomplished prior to ECMAScript 5?

查看:115
本文介绍了如何在ECMAScript 5之前完成第二个参数的Object.create()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当处理 Object.create()时,我遇到了这个问题并总结它提到:

When dealing with Object.create() I came across this question and in summary it mentions:


  • 用于改造IE8的polyfill Object.create
    功能,但此polyfill不支持第二个参数

  • shim,需要包含 es5-shim

  • polyfill that was used to retrofit IE8 with Object.create functionality, however this polyfill doesn't support a second parameter
  • shim which requires inclusion of es5-shim

关于如何最好地实现这一点的各种思路。

with various trains of thought on how to best accomplish this.

这是我使用的相关代码:

Here's my relevant code I am using:

//json_string variable is passed via php

var arrayOfOptions = jQuery.parseJSON(json_string);

var template = {
     // my instance that I want to initialize based on the properties provided
};

var instance = Object.create(template, {'options':{value:arrayOfOptions, enumerable: true}});

我的问题是程序员在ES5之前是如何完成这项任务的,更具体地说是什么会更好/基于一组传递的属性初始化对象而不使用第二个参数的替代实现?

My question is how did programmers accomplish this task prior to ES5, and more specifically what would be a better/alternative implementations of initializing an object based on on a set of passed properties without utilizing the second parameter?

推荐答案


程序员如何在ES5之前完成此任务

how did programmers accomplish this task prior to ES5

完全没有。 ES3中有属性属性,但没有办法设置它们。也没有 Object.create 。刚刚 .propertyIsEnumerable() 获取 DontEnum 标志。

Not at all. There had been property attributes in ES3, but there was no way to set them. There was no Object.create as well. There just had been .propertyIsEnumerable() to get the DontEnum flag.


什么会是一个更好的/替代实现基于一组传递的属性初始化对象而不使用第二个参数?

what would be a better/alternative implementations of initializing an object based on on a set of passed properties without utilizing the second parameter?

使用 extend 函数。 jQuery示例:

Use an extend function. Example with jQuery:

var instance = $.extend(Object.create(template), {options:arrayOfOptions});

自制助手功能示例:

function createAndExtend(proto, props) {
    var o = Object.create(proto);
    for (var p in props)
        o[p] = props[p];
    return o;
}
var instance = createAndExtend(template, {options:arrayOfOptions});

无论如何你几乎不需要属性描述符。

You hardly ever need property descriptors anyway.

这篇关于如何在ECMAScript 5之前完成第二个参数的Object.create()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆