当前第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可以创建对象吗的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
浅谈javascript中数组array的添加/删除操作
快速了解ui组件功能设计
js中==和===的区别是什么
javascript诞生于哪一年
javascript void0怎么解决
一起来聊聊jquery、javascript与js三者间的区别
javascript中取余怎么算
手把手教你理解js中的执行上下文
java和javascript一样么
javascript语法是什么
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript不使用new可以创建对象吗