当前第2页 返回上一页
1 2 3 4 5 6 7 8 9 10 11 12 13 | var newObj = Object.create ( null , {
size : {
value : "large" ,
enumerable : true
},
shape : {
value : "round" ,
enumerable : true
}
});
console.log(newObj.size);
console.log(newObj.shape);
console.log(Object.getPrototypeOf(newObj));
|
示例2
下面示例使用 Object.create 定义一个与对象直接量具有相同原型的对象。
1 2 3 4 5 6 7 8 9 10 | var obj = Object.create(Object.prototype, {
x : {
value : undefined,
writable : true ,
configurable : true ,
enumerable : true
}
});
console.log( "obj.prototype = " + Object.getPrototypeOf(obj));
Object.getPrototypeOf() 函数可获取原始对象的原型。如果要获取对象的属性描述符,可以使用 Object.getOwnPropertyDescriptor() 函数。
|
示例3
下面示例定义一个对象,使用访问器属性 b 来读写数据属性 a。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var obj = Object.create(Object.prototype, {
a : {
writable : true,
value : "a"
},
b : {
get : function () {
return this.a;
},
set : function (value) {
this.a = value;
},
}
});
console.log(obj.a);
console.log(obj.b);
obj.b = 20;
console.log(obj.b);
|
【相关推荐:javascript学习教程】
以上就是javascript不使用new可以创建对象吗的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
c和javascript区别有哪些
javascript中有哪三种对象
http缓存机制的简单介绍
用h5的webgl如何在同一个界面做出json和echarts图表
javascript获取日期的方法是什么
一份window.location的备忘单,助你更好理解决地址路径问题!!
javascript怎么删除行
javascript如何关闭子窗口
javascript中怎么单行注释
javascript怎么改变src属性值
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript不使用new可以创建对象吗