_.extend
The method is used to copy the values of all enumerable own and inherited properties from one or more source objects to a target object.
- // Underscore
- // Lodash: _.assignIn
- function Foo() {
- this.c = 3;
- }
- function Bar() {
- this.e = 5;
- }
- Foo.prototype.d = 4;
- Bar.prototype.f = 6;
- var result = _.extend({}, new Foo, new Bar);
- console.log(result);
- // output: { 'c': 3, 'd': 4, 'e': 5, 'f': 6 }
- // Native
- function Foo() {
- this.c = 3;
- }
- function Bar() {
- this.e = 5;
- }
- Foo.prototype.d = 4;
- Bar.prototype.f = 6;
- var result = Object.assign({}, new Foo, Foo.prototype, new Bar, Bar.prototype);
- console.log(result);
- // output: { 'c': 3, 'd': 4, 'e': 5, 'f': 6 }
- //Or using a function
- const extend = (target, ...sources) => {
- let source = [];
- sources.forEach(src => {
- source = source.concat([src, Object.getPrototypeOf(src)])
- })
- return Object.assign(target, ...source)
- };
- console.log(extend({}, new Foo, new Bar));
- // output: { 'c': 3, 'd': 4, 'e': 5, 'f': 6 }
Browser Support for Object.assign()
45.0 ✔ | ✔ | 34.0 ✔ | ✖ | 32.0 ✔ | 9.0 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .