Object.create()和new 创建对象的区别
区别 object create New 创建对象
2023-09-11 14:20:08 时间
Object.create()方法是ECMAScript5中新增的,用来规范化原型式继承的。
这个方法接收两个参数,一个是用作新对象原型的对象,和一个为新对象定义额外属性的(可选)对象。
var person = {
name: "Nicholas",
friends: ["John", "Jane"] // 引用类型值属性共享
}
var onePerson = Object.create(person); // onePerson继承person对象
console.log(onePerson) //{},自己空对象,可以通过隐士原型继承person
console.log(onePerson.name,onePerson.friends ) //Nicholas , ["John", "Jane"]
onePerson.name = "Greg";
onePerson.friends.push("Mike");
console.log(onePerson.name); //onePerson自己添加的name属性 Greg
console.log(onePerson.friends); //修改person原型对象的friends ["John", "Jane", "Mike"]
console.log(onePerson); //{name: "Greg"}
// 第二个参数对象格式与Object.defineProperties()方法的第二个参数格式相同
var theOtherPerson = Object.create(person, {
name : {
configurable : false, // 不可修改
value : "Greg"
}
});
console.log(theOtherPerson.name); // Greg
theOtherPerson.name = "Bob"; // 失效
console.log(theOtherPerson.name); // Greg
new Object()方法的实质是,使用引用类型Object的构造函数创建了一个新的实例,这个实例拥有Object默认的方法如toString、toLocaleString等。
相关文章
- [Git] git merge和rebase的区别
- String s=new String("xyz");创建了几个String Object?二者之前的区别是什么?
- FTP协议的主动模式和被动模式的区别
- Cannot assign to read only property 'exports' of object '#<Object>'
- [ES2017] Iterate over properties of an object with ES2017 Object.entries()
- 微信小程序----wx.scanCode(Object object)调起客户端扫码界面进行扫码
- SAP Spartacus 服务器端渲染 timeout 和 forcedSsrTimeout 两个参数的区别
- Angular 依赖注入 UseClass 和 UseExisting 的区别 - 一个实际的测试例子
- Angular页面调试一个有用的小技巧 - normalizeDebugBindingName和normalizeDebugBindingValue - [object Object]
- Object reference not set to an instance of an object.
- 微信小程序wx.previewImage(Object object)真机调试无法全屏预览图片
- 深入【Get】与【Post】区别
- java.lang.reflect.Filed.class中setInt与set的区别
- DBI接口和DPI接口的区别
- Java构造器和方法的区别
- Mysql 中 int(3) 和 int(11) 的区别
- Python中threading的join和setDaemon的区别及用法
- Dart中dynamic,var,object三者的区别
- cid unmatched [object Object] at view.umd.min.js:1 TypeError: Invalid attempt to destructure non-ite
- LoadRunner——HTML和URL录制方式的区别(十一)