1.ES5的属性特性包括下面六个:
configurable: 表示能否通过delete来删除属性从而重新定义属性,能够修改属性的特性,默认为true
enumberable: 表示是否能通过for-in循环返回属性。默认为true
writable: 是否可以修改属性, 默认为true
value: 包含这个属性的数据值。读取属性值时3,从这个属性读,写入属性时,把新值保存到这个位置。默认值为undefine.
getter: 在读取属性时,调用的函数
setter: 在写入属性时调用的函数
特别注意:一旦调用了Object.defineProperty方法之后,那些未定义的特性值除了configurable为false之外,其他都为undefined;
2.修改单个属性的特性:
必须使用Object.defineProperty()方法。这个方法接受3个参数: 属性所在的对象,属性的名字,和一个描述符对象。其中描述符对象的属性必须是:configurable, enumberable, writable,value.设置其中一个或者多个值,可以修改对应的特性值。
修改writable特性,设置为false从而使得属性值不可修改,如下所示:
var person = {};
Object.defineProperty(person, "name", {
writable: false,//设置属性的特性为不可修改的
value: "Tom" //把name属性值设为Tom
});
person.name = "Jany"; //试图把name属性值设置为Jany,但是失败了.非严格模式下会忽略此语句,严格模式则会报错的
console.log(person.name); //结果仍然为Tom
第2到第6行我们设置了name属性的值为Tom,并且把它的writable特性设置为false,然后,我们试图在地7行把它改Jany,但是,最终打印出来的结果仍然是zzy。
修改configurabel特性,设置为false使得属性无法被delete(删除),如下所示:
var person = {};
Object.defineProperty(person, "name", {
configurable: false, //把configurable设置为false
value: "Tom"
});
console.log(person.name); //打印结果为Tom
delete person.name; //试图删除name属性
console.log(person.name); //但是name仍然存在,打印出Tom
如果我们把configurable属性值改为true,则属性可以被delete
var person = {};
Object.defineProperty(person, "name", {
configurable: true,
value: "Tom"
});
console.log(person.name); //打印出Tom
delete person.name; //试图删除name属性
console.log(person.name); //删除成功,打印出udefined
另外,一旦,我们我们把某个属性的特性设置成了不可配置的,就再也不能把它变回可配置的了。此时,调用Object.defineProperty()方法修改除了writable之外的特性都会报错。
如下:
var person = {};
//把person.name属性的configurable设置为false;
Object.defineProperty(person, "name", {
configurable: false,
value: "Tome"
});
//然后试图重新把person.name属性的configurable设置为true;但是浏览器出现报错信息。
Object.defineProperty(person, "name", {
configurable: true,
value: "lyl"
}); console.log(person.name);
我们在浏览器会看到如下的报错信息:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAuUAAAA3CAIAAAAkMaOkAAAOkElEQVR4nO2dPWwbRxbHp0sX6JpUKeziCoMFiZyNAwwITiohgIEDUuzhwGMVKWWqnHBCAOLAJtzWCNKwOGGLsAgIs0yKXHKgAEcSYgNeyZYgAYYc+y4wi0iyaZsnk3PFzM7O135R/FhJ/8UPxmq5O/Pe7FjvzzdvV+Tw8JB2uwAAAAAAueK/RycCEqVXjjc391dW/IWFnfn5h1ev+gsL+ysrRxsbM7ceAAAAABeBZL3ydHV1+/r1Q0LeEEIJoYS8IeSQkO3r159+883MHQAAAADAuSdBrzxdXb33zjtCqci8IeTuW289XV2duQ8zYL1GSXkMjZRqkZ82y3yom7N2Nj3jtdkcn/gRyxW1Ei03aZnQ2vqZaRkAAHJMnF452tjYvn79DSG02aTff09LJR6N5ufpt9/S27ffELJ95crx5mZE6+u0ZAid0X7JrtfUdkp0faYDJwLnaSJo3LVN7uMZitBjtzmfeiWlDc0yLTdprTR+uTm5lgEAIMfE6ZX9lZVDpg++/JJSSn/5hV66ROfmaKdDKaVff00JOSRkf2UloZvTh5k8BCqF5mT1yljyN1Nm7Dafdb1SW5+UXplQywCcLTonRUKJ06fdnlugpHCyNYH2i9XeKRrpuQVKyKCd1EvFC370BoQM3c74vPAGpHCyxS2RcPrpWuhX5EHQrM02DhLBzWo7GW5cnF7xFxbClaAff6SU0nv36O3blFJ65w6dm6OEDAjxFxYSulF+xa/TklgvaFJSos0aLZXDTEwzuEROpUSFrjKhpKzsK9eW9TP5p2Z6phkOZLNLayVL72XVQuGLljeKNCboVM4VCdGjnNNUbmypZjfAbFk3wHpyOfKcmAFJvtyw2fRUvolsXxsry7CLiaFOgzjf1bmUPDfSzT3egnzHm/pMtub8rEMd42kz6CL+LgNwjvEGRAqKbScq5Pcro+iV1CdH6JVoeyL6sp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c+tKeUpwmlAAQVGFteKBnc++hbMTzOeWw/qVAlGXbwJJxL/BB+FZ/dLPLq974Y7Wgty7YqHklFq/QrueW3eKiheKm5YWutaOzHUZkWLJ3IW5qmXUr6jjEP04uh17C4TwgpWC23D4beJlLnzFUMkJWdpMYwP0CgAA5JEpvS+OSZZjSakwsTL49dcRm9Xj+vlELSgefaAisiNOUIc78aKNPDw+ndIGvC8OAAByyPTex98/OOjt7h5vbx9vb/d2d/sHB3i57YVgrVpcHPc7/c6iDQAAAE4B/t4hAAAAAPJO3vXKvzd/uPy3mz9u/GvmlgAAAABgVuRar9z5ufOHf/yFLF37/d//dOdnJPMBAACAC0p+9crd+z/9sfZXsnSNUar++e79n2ZuFQAAAACmT371ypXPP3r3sw8vL9/83acfXF6++e5nH175/KOZWwUAAACA6ZNfvXKwt80gS9fE/sytAgAAAMD0ya9eEZClazO3AQAAAAAzJO965bfHj6BXAAAAgAtOrvXKb48f/fO75vtffDxzSwAAAAAwQ/KrV8jStblPP3j/i48foWwFAAAAuNjkV68AAAAAADCgVwAAAACQd6BXAAAAAJB3oFcAAAAAkHegVwAAAACQd6BXAAAAAJB3oFcAAAAAkHegVwAAAACQd6BXAAAAAJB3oFcAAAAAkHd0vfIQGzZs2LBhm9g287AHzhDyzDHyK9iwYcOGDdvktlmHQHCWkDboFWzYsGHDhg1b3jdZr/wfoOMSwczUiRMAAAAASUVORK5CYII=" alt="" />
修改getter和setter特性
//定义一个对象book
var book = {
_year: 2004, //出版年份
edition: 1 //版本号
};
//定义year属性的特性
Object.defineProperty(book, "year", {
//重写get方法,即如果我们试图book.year获取year值时,实质上是获取book._year的值
get: function() {
return this._year;
},
//重写了set方法,每当对book.year进行赋值时,则相应的修改book._year和book.edition的值
set: function(newValue) {
if(newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
} }); book.year = 2005;
console.log(book.edition); //
3.同时定义(修改)多个属性的特性
由于定义多个属性的可能性很大,因此ES5有定义一个Object.defineProperties方法。利用这个方法可以通过描述符一次定义多个属性。这个方法接受两个对象的参数;第一个对象是要添加和修改器属性的对象,第二个对象的属性与第一个对象中要添加或者修改的顺序一一对应。
var book = {};
Object.defineProperties(book, {
// 定义一个新的属性_year,并且他的值为2004
_year: {
value: 2004
},
// 定义一个新的属性edition,并且他的默认值是1,而且属性值是可修改的
edition: {
value: 1,
writable: true// 可修改的
},
// 定义一个新的属性year,并且重写了他的get和set方法
year: {
get: function() {
return this._year;
},
set: function(newValue){
if(newValue > 2004) {
//alert("ok");
this._year = newValue;
this.edition += newValue-2004;
}
}
}
}); book.year = 2006;
console.log(book.edition);//
4.读取属性的特性
读取属性的特性,使用Object.getOwnPropertyDescriptor方法
var book = {};
// 为该对象定义一些属性
Object.defineProperties(book, {
_year: {
value: 2004
},
edition: {
value: 1,
writable: true// 可修改的
},
year: {
get: function() {
return this._year;
},
set: function(newValue){
if(newValue > 2004) {
//alert("ok");
this._year = newValue;
this.edition += newValue-2004;
}
}
}
}); //获取_year属性的特性
var descriptor1 = Object.getOwnPropertyDescriptor(book, "_year");
console.log(descriptor1.configurable); //false
console.log(descriptor1.enumberable); //undefinded
console.log(descriptor1.writable); //false
console.log(descriptor1.value); //
console.log(descriptor1.get); //undefined
console.log(descriptor1.set); //undefined console.log("\n"); //获取year属性的特性
var descriptor2 = Object.getOwnPropertyDescriptor(book, "year");
console.log(descriptor2.configurable); //false
console.log(descriptor2.enumberable); //undefined
console.log(descriptor2.writable); //undefined
console.log(descriptor2.value); //undefined
console.log(descriptor2.get); //function()
console.log(descriptor2.set); //function(value)