1.Object.create() 是什么
1.Object.create() 是什么?
Object.create(proto [, propertiesObject ]) 是E5中提出的一种新的对象创建方式,第一个参数是要继承的原型,如果不是一个子函数,可以传一个null,第二个参数是对象的属性描述符,这个参数是可选的。
例如:
function Car (desc) {
this.desc = desc;
this.color = "red";
}
Car.prototype = {
getInfo: function() {
return 'A ' + this.color + ' ' + this.desc + '.';
}
};
//instantiate object using the constructor function
var car = Object.create(Car.prototype);
car.color = "blue";
alert(car.getInfo());
结果为:A blue undefined.
2.propertiesObject 参数的详细解释:(默认都为false)
数据属性
- writable:是否可任意写
- configurable:是否能够删除,是否能够被修改
- enumerable:是否能用 for in 枚举
- value:值
访问属性:
- get(): 访问
- set(): 设置
3.例子:直接看例子就知道怎么用。
<!DOCTYPE html>
<html>
<head>
<title>yupeng's document </title>
<meta charset="utf-8"/>
</head>
<body>
<script type="text/javascript">
var obj = {
a:function(){
console.log(100)
},
b:function(){
console.log(200)
},
c:function(){
console.log(300)
}
}
var newObj = {};
newObj = Object.create(obj,{
t1:{
value:'yupeng',
writable:true
},
bar: {
configurable: false,
get: function() { return bar; },
set: function(value) { bar=value }
}
})
console.log(newObj.a());
console.log(newObj.t1);
newObj.t1='yupeng1'
console.log(newObj.t1);
newObj.bar=201;
console.log(newObj.bar)
function Parent() { }
var parent = new Parent();
var child = Object.create(parent, {
dataDescriptor: {
value: "This property uses this string as its value.",
writable: true,
enumerable: true
},
accessorDescriptor: {
get: function () { return "I am returning: " + accessorDescriptor; },
set: function (val) { accessorDescriptor = val; },
configurable: true
}
});
child.accessorDescriptor = 'YUPENG';
console.log(child.accessorDescriptor);
var Car2 = function(){
this.name = 'aaaaaa'
} //this is an empty object, like {}
Car2.prototype = {
getInfo: function() {
return 'A ' + this.color + ' ' + this.desc + '.';
}
};
var newCar = new Car2();
var car2 = Object.create(newCar, {
//value properties
color: { writable: true, configurable:true, value: 'red' },
//concrete desc value
rawDesc: { writable: true, configurable:true, value: 'Porsche boxter' },
// data properties (assigned using getters and setters)
desc: {
configurable:true,
get: function () { return this.rawDesc.toUpperCase(); },
set: function (value) { this.rawDesc = value.toLowerCase(); }
}
});
car2.color = 'blue';
console.log(car2.getInfo());
car2.desc = "XXXXXXXX";
console.log(car2.getInfo());
console.log(car2.name);
</script>
</body>
</html>
结果为:
转载于:https://www.cnblogs.com/jianxingjianyuan/p/10814745.html
1.Object.create() 是什么相关推荐
- object.create()
语法: Object.create(proto, [propertiesObject]) //方法创建一个新对象,使用现有的对象来提供新创建的对象的proto. 参数: proto : 必须.表示新建 ...
- 关于Object.create()与原型链的面试题?
原文地址 https://segmentfault.com/q/1010000004670616?utm_source=weekly&utm_medium=email&utm_camp ...
- Object.create(..)和new(..)的内部实现
Object.create() 经常会有这样的疑问?Object.create()到底做了什么工作? 像这样两行代码有什么不同? var obj ={a: 1} var b = obj var c = ...
- js-new、object.create、bind的模拟实现【转载备忘】
//创建Person构造函数,参数为name,age function Person(name,age){this.name = name;this.age = age; }function _new ...
- 原生js实现Object.assign和Object.create
Object.assign(target,-sources) Object.assign(target,-source)方法用于将所有可枚举的属性的值从一个或多个源对象复制到目标对象.它将返回目标对象 ...
- Object.create()和Object.assign()
一.Object.create() 该方法用于创建一个新对象,并为其指定原型对象和属性. 语法:Object.create(prototype,description); proto:(必须),表示新 ...
- Object.create()和new Object()
Object.create(null) 创建的对象是一个空对象,在该对象上没有继承 Object.prototype 原型链上的属性或者方法,例如:toString(), hasOwnProperty ...
- Object.create()方法
Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的proto. 语法:Object.create(proto, [propertiesObject]) - proto ...
- js学习日记-new Object和Object.create到底干了啥
function Car () {this.color = "red"; } Car.prototype.sayHi=function(){console.log('你好') }v ...
- firefox-Developer开发者站点——关于Object.create()新方法的介绍
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create Objec ...
最新文章
- PTA 基础编程题目集 7-11 分段计算居民水费 C语言
- 云场景实践研究第34期:加和科技
- 【渝粤教育】国家开放大学2019年春季 8042-22T养殖业基础 参考试题
- [渝粤教育] 西南科技大学 管理信息系统 在线考试复习资料(1)
- rabbitmq topic 收不到数据_RabbitMQ和Kafka到底怎么选?
- Codeforces 1187E - Tree Painting(树上所有节点的儿子数量和最大)
- 《位置计算:无线网络定位》学习小结
- bzoj 1047 理想的正方形
- C++学生信息管理系统5.0
- OSPF的多域配置-要点总结
- 东芝Toshiba DP-3003 一体机驱动
- oracle11g和toad,plsql、toad应用于oracle11g64位的方法
- JVM虚拟机与Android虚拟机
- ReactNative Animated动画详解
- pythoncookie自动登录_Python使用cookie 免密登录了解一下
- java队名_求JAVA团队名称
- 删除word前面几页的页眉 保留后面的页眉
- 如何利用动态代理IP来做SEO(网络营销)
- 【世界观】硅谷最重要文件:Netflix126页PPT完全汉化版
- 忘记电子商务吧,很快一切都将与元宇宙有关