Object的方法
1、Object.assign()
方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。 ES2015引入的
,且可用polyfilled。要支持旧浏览器的话,可用使用jQuery.extend或者_.assign()。
2、Object.create()
方法会使用指定的原型对象及其属性去创建一个新的对象。
语法:
Object.create(proto[, propertiesObject])
实现类式继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function MyClass() { SuperClass.call( this ); OtherSuperClass.call( this ); } // 继承一个类 MyClass.prototype = Object.create(SuperClass.prototype); // 混合其它 Object.assign(MyClass.prototype, OtherSuperClass.prototype); // 重新指定constructor MyClass.prototype.constructor = MyClass; MyClass.prototype.myMethod = function () { // do a thing }; |
使用 Object.create
的 propertyObject
参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | var o; // 创建一个原型为null的空对象 o = Object.create( null ); o = {}; // 以字面量方式创建的空对象就相当于: o = Object.create(Object.prototype); o = Object.create(Object.prototype, { // foo会成为所创建对象的数据属性 foo: { writable: true , configurable: true , value: "hello" }, // bar会成为所创建对象的访问器属性 bar: { configurable: false , get: function () { return 10 }, set: function (value) { console.log( "Setting `o.bar` to" , value); } } }); function Constructor(){} o = new Constructor(); // 上面的一句就相当于: o = Object.create(Constructor.prototype); // 当然,如果在Constructor函数中有一些初始化代码,Object.create不能执行那些代码 // 创建一个以另一个空对象为原型,且拥有一个属性p的对象 o = Object.create({}, { p: { value: 42 } }) // 省略了的属性特性默认为false,所以属性p是不可写,不可枚举,不可配置的: o.p = 24 o.p //42 o.q = 12 for ( var prop in o) { console.log(prop) } //"q" delete o.p //false //创建一个可写的,可枚举的,可配置的属性p o2 = Object.create({}, { p: { value: 42, writable: true , enumerable: true , configurable: true } }); |
Polyfill
这个 polyfill 涵盖了主要的应用场景,它创建一个已经选择了原型的新对象,但没有把第二个参数考虑在内。
请注意,尽管在 ES5 中 Object.create
支持设置为[[Prototype]]
为null
,但因为那些ECMAScript5以前版本限制,此 polyfill 无法支持该特性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | if ( typeof Object.create !== "function" ) { Object.create = function (proto, propertiesObject) { if ( typeof proto !== 'object' && typeof proto !== 'function' ) { throw new TypeError( 'Object prototype may only be an Object: ' + proto); } else if (proto === null ) { throw new Error( "This browser's implementation of Object.create is a shim and doesn't support 'null' as the first argument." ); } if ( typeof propertiesObject != 'undefined' ) throw new Error( "This browser's implementation of Object.create is a shim and doesn't support a second argument." ); function F() {} F.prototype = proto; return new F(); }; } |
_.assign
js:https://lodash.com/vendor/cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js
1 2 3 4 5 6 7 8 9 10 11 12 13 | function Foo() { this .a = 1; } function Bar() { this .c = 3; } Foo.prototype.b = 2; Bar.prototype.d = 4; _.assign({ 'a' : 0 }, new Foo, new Bar); // => { 'a': 1, 'c': 3 } |
_.assignIn
1 2 3 4 5 6 7 8 9 10 11 12 13 | function Foo() { this .a = 1; } function Bar() { this .c = 3; } Foo.prototype.b = 2; Bar.prototype.d = 4; _.assignIn({ 'a' : 0 }, new Foo, new Bar); // => { 'a': 1, 'b': 2, 'c': 3, 'd': 4 } |
你要觉得这篇文章比较好,记得点推荐!
标签:
JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本