概述
//使用字面量语法创建对象
var teacher = {
name:"Jaki",
age:25,
teaching:function(){
console.log("teching...");
}
};
Object.defineProperties(teacher,{
"name":{
value:"珲少",
writable:false
},
"age":{
value:25,
writable:false
}
});
//使用Object构造函数创建对象
var student = new Object();
student.name = "Lucy";
student.age = 24;
student.learning = function(){
console.log("learning...");
};
var name = "Lucy";
Object.defineProperty(student,"name",{
configurable:true,
enumerable:true,
// writable:true,
// value:"Lucy",
get:function(){
console.log("正在使用name属性");
return name;
},
set:function(value){
console.log("将要设置name属性");
name = value;
}
});
console.log(student.name);//正在使用name属性 Lucy
student.name = "July";//将要设置name属性
console.log(student.name);//正在使用name属性 July
var prop = Object.getOwnPropertyDescriptor(teacher,"name");
console.log(prop);
2 Object对象的assign,对象复制:
var teacher = {
name:"jaki",
age:24,
teaching:function(){
console.log("teaching");
}
};
var teacher2 = {
subject:"JavaScript"
}
Object.defineProperty(teacher,"number",{
value:1001,
enumerable:false
});
console.log(teacher.number);//1001
for(prop in teacher){
console.log(prop);
}
//进行对象可枚举属性的复制
var obj = {};
//第1个参数为目标对象 其后的参数被要被拷贝属性的对象
Object.assign(obj,teacher,teacher2);
console.log(obj.name+obj.age+obj.subject);//jaki24JavaScript
obj.teaching();//teaching
console.log(obj.number);//undefined
深浅拷贝问题:
//深浅拷贝
var obj1 = {
a:{
name:"Jaki"
},
b:25
};
var obj2 = {};
Object.assign(obj2,obj1);
//修改obj1
obj1.a.name = "Lucy";
obj1.b = 23;
//obj2中的b并没有被修改 因为其是原始值类型 但是a属性被修改了 因为其是引用类型
console.log(obj2);//{ a: { name: 'Lucy' }, b: 25 }
3 Object对象的继承问题:
var base = {
subject:"JavaScript"
}
var teacher1 = Object.create(base,{
"name":{
value:"Jaki",
enumerable:true
},
"age":{
value:25,
enumerable:true
}
});
console.log(Object.keys(teacher1));//[ 'name', 'age' ]
console.log(Object.getPrototypeOf(teacher1));//{ subject: 'JavaScript' } //获取原型对象
console.log(Object.getOwnPropertyNames(teacher1));//[ 'name', 'age' ] //获取自身的属性
console.log(teacher1);//{ name: 'Jaki', age: 25 }
console.log(teacher1.subject);//JavaScript
var fre = {
name:"Jaki"
};
fre = Object.freeze(fre);
fre.name = "Lucy";
//冻结的对象不能修改
console.log(fre);//{ name: 'Jaki' }
var seal = {
name:"Jaki"
};
self = Object.seal(seal);
//密封对象不能添加新属性
seal.age = 25;//undefined
console.log(seal.age);
var ext = {
name:"Jaki"
}
//抑制对象扩展
ext = Object.preventExtensions(ext);
ext.age = 25;
console.log(ext.age);//undefined
4 Object对象常用实例方法:
var teacher = new Object();
var prototype = {
subject:"JavaScript"
};
//检查对象的某个属性是否为可枚举的
console.log(prototype.propertyIsEnumerable("subject"));//true
//设置原型
Object.setPrototypeOf(teacher,prototype);
teacher.name = "Jaki";
teacher.age = 25;
teacher.teaching = function(){
console.log("teaching");
}
console.log(prototype.isPrototypeOf(teacher));//true
//判断某个对象本身是否包含指定的属性 此属性不是原型链上的
console.log(teacher.hasOwnProperty("name"));//true
最后
以上就是热情手机为你收集整理的javascript对象中的object对象的全部内容,希望文章能够帮你解决javascript对象中的object对象所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复