从另一个对象扩展而不使用object.JavaScript中的assign (ES6特性)
Extending from another object without using Object.assign (ES6 feature) in JavaScript
更新1:我之前没有提到这一点,但是我试图避免使用new关键字。Polyfill应该工作得很好。我应该清楚地表明,我是在寻找是否有另一种方法来编写类似的代码。
Chris Elliott的文章《关于JS中继承的常见误解》阐述了一种使用闭包来保护数据隐私的好方法,但使用的是ES6方法Object.assign。
他这样描述:
Object.assign()
是由Rick Waldron倡导的ES6新特性,以前在几十个库中实现过。你可能知道它是来自jQuery的$.extend()
或来自Underscore的_.extend()
。Lodash有一个叫做assign()
的版本。传入一个目标对象和任意数量的源对象,它们之间用逗号分隔。它将通过赋值复制所有可枚举的自身属性以last为优先级的源对象到目标对象。如果存在任何属性名称冲突,则从上一个版本开始
我如何使用ES6特性Object.assign编写这个示例而不使用 ?
let animal = {
animalType: 'animal',
describe () {
return `An ${this.animalType} with ${this.furColor} fur,
${this.legs} legs, and a ${this.tail} tail.`;
}
};
let mouseFactory = function mouseFactory () {
let secret = 'secret agent';
return Object.assign(Object.create(animal), {
animalType: 'mouse',
furColor: 'brown',
legs: 4,
tail: 'long, skinny',
profession () {
return secret;
}
});
};
let james = mouseFactory();
通过编写自己版本的object.assign!
function assign(target, sources) {
// for each object in source, iterate through properties
// check has own property and add the property to the target
// object
// or just modify in place, too many different ways to do this
let newTarget = new target();
for (let i = 0; i < sources.length; i++) {
for (let key in sources[i]) {
if (sources[i].hasOwnProperty(key)) {
newTarget[key] = sources[i][key];
}
}
}
return newTarget;
}
编辑:你绝对不需要使用new——你也可以把它们添加到一个空对象中。
function assign(sources) {
// for each object in source, iterate through properties
// check has own property and add the property to the target
// object
// this is not the important part
let newTarget = {};
for (let i = 0; i < sources.length; i++) {
for (let key in sources[i]) {
if (sources[i].hasOwnProperty(key)) {
newTarget[key] = sources[i][key];
}
}
}
return newTarget;
}
你可以直接使用Object.create
也就是ES5,如下所示:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
我已经把你的代码重写成这样的格式,好了:
var animal = {
animalType: 'animal',
describe: function() {
return 'An ' + this.animalType + ' with ' + this.furColor + ' fur, '
+ this.legs + ' legs, and a ' + this.tail + ' tail.';
}
};
var mouseFactory = function mouseFactory () {
var secret = 'secret agent';
return Object.create(animal, {
animalType: {
value: 'mouse',
writable: true
},
furColor: {
value: 'brown',
writable: true
},
legs: {
value: 4,
writable: true
},
tail: {
value: 'long, skinny',
writable: true
},
profession: {
value: function () {
return secret;
}
}
});
};
var james = mouseFactory();
var dave = mouseFactory();
dave.furColor = 'white';
console.log(james.describe());
// An mouse with brown fur, 4 legs, and a long, skinny tail.
console.log(dave.describe());
// An mouse with white fur, 4 legs, and a long, skinny tail.
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 可以't让我的if语句处理js中的html表单输入
- 引用对象中的通用值
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 用程序搜索JQuery数据表中的文本
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 不能从angular2中的子组件指定父组件中的数组
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如何更改bigquery API中的计费层选项
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 使用object.assign()添加到对象中的getter发现闭包变量的值错误
- 如何覆盖JavaScript中的location.assign或location.replace函数
- 从另一个对象扩展而不使用object.JavaScript中的assign (ES6特性)
- 什么's相当于Javascript's C#中的Object.assign()
- Object.assign以更新对象中的数组元素