js中对象的属性使用

js中对象的属性使用

对象包含一系列属性,且这些属性是无序的,每个属性是由key:value

var obj = {}
obj[1]=2;
obj["1"] = 3;
obj;

Object {1: 3}

var obj = {}
obj[{}]=2;
obj[{x:1}] = 3;
obj;

Object {[object Object]: 3}

对象的每一个元素的属性标签:

  • writeable
  • enumerable
  • configurable:是否可配置
  • value
  • get
  • set
  • prototype

1: prototype原型链

function Foo(){}
Foo.prototype.z=1
var foo = new Foo()
console.log(foo.z)//1
typeof foo.toString;//function
"z" in foo;//true
foo.hasOwnProperty("z")//false
foo.z = 6
foo.hasOwnProperty("z")//true
foo.prototype.z;// 1
foo.z//6
foo.z = undefined;
foo.z//undefined
delete foo.z;
foo.z//1

对象创建

var obj = Object.create({x:1});
obj.x //1
typeof obj.toString//"function"
obj.hasOwnProperty("x")//false

var obj = Object.create(null);
obj.toString//undefined

2:属性读写

obj.x
obj[x]

3:属性删除

<script>
	var person = {name:"zyb",age:23};
	delete person.name;//true
	delete Object.prototype //false
</script>

var descriptor = Object.getOwnPropertyDescriptor(Object,"prototype");
console.log(descriptor.configurable)//false

变量 函数不可以删除(不算全局还是局部都一样)

var a = 1;
delete a //false

(function(){
	var a = 1;
	return delete a //false
})()

function a(){}
delete a//false

(function(){
	function b(){}
	return delete b//false
})()

a = 1;
window.a;
delete a;//true

eval("var a = 1")
delete a//true

4: 属性检测

var person = new Object();
person.name = "zyb"

"name" in person //true
"toString" in person //true

person.hasOwnProperty("name")//true
person.hasOwnProperty("toString")//false

person.propertyIsEnumerable("name")//true
person.propertyIsEnumerable("toString")//false

var cat = {};
Object.defineProperty(cat,"name",{rnumerable:false,value:"haha"})
cat.propertyIsEnumerable("name");//false
cat.hasOwnProperty("name")//true

5:属性的枚举

var person = {x:1,y:2,z:3}
"toString" in person //true
person.propertyIsEnumerable("toString")//false
for(var i in person){
	console.log(i) //x,y,z
}

var obj = Object.create(person);
obj.a = 4;
for(var i in obj){
	console.log(i)//a,x,y,z
}


for(var i in obj){
	if(obj.hasOwnProperty(i)){
		console.log(i)//a
	}
}

6:get/set

var person = {
	name:"zyb",
	get age(){
		return new Date().getFullYear()-1987
	},
	set age(val){
		console.log("不能设置:"+val)	
	}
}

console.log(person.name)//zyb
console.log(person.age)//29
person.age = 100//不能设置:100
console.log(person.age)//29

var man = {
	name:"zansan",
	$age:null,
	get age(){
		if(this.$age == undefined){
			return new Date().getFullYear()-1987 
		}else{
			return this.$age
		}
	},
	set age(val){
		val =+ val;
		if(!isNaN(val) && val > 0 && val <100){
			this.$age = +val;
		}else{
			throw new Error("Incorrect val = "+val)
		}
	}
}

console.log(man.name);//zansan
console.log(man.age);//29
console.log(man.$age);//null
man.age = 50;
console.log(man.age);//50
man.age = "abc"//Uncaught Error: Incorrect val = NaN

function foo(){}
Object.defineProperty(foo.prototype,"z",{get:function(){return 1;}})
var obj = new foo();
obj.z//1
obj.z =100;
obj.z//1

Object.defineProperty(obj,"z",{value:100,configurable:true})
obj.z//100
delete obj.z;
obj.z//1

var o = {}
Object.defineProperty(o,"x",{value:1});
var obj = Object.create(o);
obj.x//1
obj.x = 200;
obj.x//1

Object.defineProperty(obj,"x",{writable:true,configurable:true,value:100});
obj.x//100
obj.x = 500;
obj.x//500

7:属性标签

Object.getOwnPropertyDescriptor({pro:true},"pro")
Object {value: true, writable: true, enumerable: true, configurable: true}

Object.getOwnPropertyDescriptor({pro:true},"a")
undefined

var person = {};
Object.defineProperty(person,"name",{
	configurable:false,
writable:false,
enumerable:true,
value:"zyb"
})

person.name//zyb
person.name = "123",
person.name//zyb
delete person.name//false

Object.defineProperty(person,"type",{
	configurable:true,
	writable:true,
	enumerable:false,
	value:"Object"
})

Object.keys(person) ["name"]

Object.defineProperties(person,{
	title:{value:"fe",enumerable:true},
	name:{value:"tom",enumerable:true},
	age:{value:"1000",enumerable:true,writable:true,},
	luck:{
		get:function(){
			return Math.random() > 0.5 ? "good":"bad";
		}
	},
	promote:{
		set:function(level){
			this.age *= 1+level*0.1
		}
	}
})

Object.getOwnPropertyDescriptor(person,"title")
//Object {value: "fe", writable: false, enumerable: true, configurable: false}
Object.getOwnPropertyDescriptor(person,"age")
//Object {value: "fe", writable: true, enumerable: true, configurable: false}

person.age//1000
person.promote = 2;
person.age//1200

修改属性的值:

       configurable:true;writable:true configurable:true;writable:false configurable:false;writable:true configurable:false;writable:false
修改属性的值 yes yes yes no
通过属性赋值修改属性的值 yes no yes no
delete该属性返回true yes yes no no
修改get/set方法 yes yes no no
修改属性标签 yes yes no no
posted on 2016-05-05 00:24  借个火点烟  阅读(568)  评论(0编辑  收藏  举报