You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Object.prototype.myCreate=function(proto,propertiesObject){// 只允许 null 或引用值if(typeofproto!=='object'){thrownewTypeError('Object prototype may only be an Object or null: '+proto)}// 直接使用字面量,无需构造函数var_obj={}_obj.__proto__=proto// 相当于 ES6 中的 Object.setPrototypeOf(_obj, proto)if(propertiesObject!==undefined){Object.defineProperties(_obj,propertiesObject)}return_obj}
如果你对里面 if (propertiesObject !== undefined) {} 存疑,不妨亲自试下这些用例:
Object.create(proto, propertiesObject)
方法会创建一个带着指定原型对象和属性的「新对象」。实现起来很简单,主要注意参数的边界值即可:
proto
- 仅接受原始值null
或任意引用值,否则抛出TypeError
。propertiesObject
- 可选参数,与Object.defineProperties()
第二参数相同。若propertiesObject
不为undefined
时,该参数「本身可枚举属性」(不包括原型链上的枚举属性)将会作为新创建对象属性值和对应的属性描述符。实现如下:
如果你对里面
if (propertiesObject !== undefined) {}
存疑,不妨亲自试下这些用例:如果
propertiesObject
参数传入原始值,执行到Object.defineProperties()
时,会先类型转换为引用值(即Object(propertiesObject)
),我们知道undefined
和null
是无法转换为引用类型的,因此会抛出TypeError
。但由于Object.create()
的第二个参数是允许指定为undefined
的,因此需要特殊处理。The end.
The text was updated successfully, but these errors were encountered: