JavaScript ES5 对象方法
JavaScript ES5 对象方法
ECMAScript 5 (2009) 向 JavaScript 添加了许多新的对象方法。
管理对象
// 以现有对象为原型创建对象 Object.create() // 添加或更改对象属性 Object.defineProperty(object, property, descriptor) // 添加或更改对象属性 Object.defineProperties(object, descriptors) // 访问属性 Object.getOwnPropertyDescriptor(object, property) // 以数组返回所有属性 Object.getOwnPropertyNames(object) // 访问原型 Object.getPrototypeOf(object) // 以数组返回可枚举属性 Object.keys(object)
保护对象
// 防止向对象添加属性 Object.preventExtensions(object) // 如果属性可以添加到对象,则返回 true Object.isExtensible(object) // 防止更改对象属性(不是值) Object.seal(object) // 如果对象被密封,则返回 true Object.isSealed(object) // 防止对对象进行任何更改 Object.freeze(object) // 如果对象被冻结,则返回 true Object.isFrozen(object)
更改属性值
语法
Object.defineProperty(object, property, {value : value})
示例代码:
此例更改属性值:
const person = { firstName: "Bill", lastName : "Gates", language : "EN" }; // 修改属性 Object.defineProperty(person, "language", {value : "NO"});
完整实例:
<!DOCTYPE html> <html> <body> <h1>更改属性值</h1> <p id="demo"></p> <script> const person = { firstName: "Bill", lastName : "Gates", language : "EN" }; // 修改属性 Object.defineProperty(person, "language", {value : "NO"}); document.getElementById("demo").innerHTML = person.language; </script> </body> </html>
运行结果:
更改属性值 NO
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
更改元数据
ES5 允许更改以下属性元数据:
writable : true // 属性值可更改 enumerable : true // 属性可枚举 configurable : true // 属性可重新配置
writable : false // 属性值不可更改 enumerable : false // 属性不可枚举 configurable : false // 属性不可重新配置
ES5 允许更改 getter 和 setter:
// 定义 getter get: function() { return language } // 定义 setter set: function(value) { language = value }
此例设置 language 为只读:
Object.defineProperty(person, "language", {writable:false});
这个例子使 language 不可枚举:
Object.defineProperty(person, "language", {enumerable:false});
列出所有属性
这个例子列出了一个对象的所有属性:
示例代码:
const person = { firstName: "Bill", lastName : "Gates", language : "EN" }; Object.defineProperty(person, "language", {enumerable:false}); Object.getOwnPropertyNames(person); // 返回数组的数组
完整实例:
<!DOCTYPE html> <html> <body> <h1>列出对象属性</h1> <p id="demo"></p> <script> const person = { firstName: "Bill", lastName : "Gates", language : "EN" }; Object.defineProperty(person, "language", {enumerable:false}); Object.getOwnPropertyNames(person); // 返回数组的数组 document.getElementById("demo").innerHTML = "My friend is : " + person.firstName + ", My brother is: " + person.lastName + ", Language is: " + person.language ; </script> </body> </html>
运行结果:
列出对象属性 My friend is : Bill, My brother is: Gates, Language is: EN
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
列出可枚举的属性
此例仅列出对象的可枚举属性:
示例代码:
const person = { firstName: "Bill", lastName : "Gates", language : "EN" }; Object.defineProperty(person, "language", {enumerable:false}); Object.keys(person); // 返回可枚举属性的数组
完整实例:
<!DOCTYPE html> <html> <body> <h1>列出可枚举的属性</h1> <p id="demo"></p> <script> const person = { firstName: "Bill", lastName : "Gates", language : "EN" }; Object.defineProperty(person, "language", {enumerable:false}); Object.keys(person); // 返回可枚举属性的数组 document.getElementById("demo").innerHTML = "My friend is : " + person.firstName + ", My brother is: " + person.lastName + ", Language is: " + person.language ; </script> </body> </html>
运行结果:
列出可枚举的属性 My friend is : Bill, My brother is: Gates, Language is: EN
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
添加属性
此例向对象添加新属性:
示例代码:
// 创建对象 const person = { firstName: "Bill", lastName : "Gates", language : "EN" }; // 添加属性 Object.defineProperty(person, "year", {value:"2008"});
完整实例:
<!DOCTYPE html> <html> <body> <h1>添加属性</h1> <p id="demo"></p> <script> // 创建对象 const person = { firstName: "Bill", lastName : "Gates", language : "EN" }; // 添加属性 Object.defineProperty(person, "year", {value:"2008"}); document.getElementById("demo").innerHTML = "My friend is : " + person.firstName + ", My brother is: " + person.lastName + ", Language is: " + person.language ; </script> </body> </html>
运行结果:
添加属性 My friend is : Bill, My brother is: Gates, Language is: EN
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
添加 Getter 和 Setter
Object.defineProperty()
方法也可以用来添加 Getter 和 Setter:
示例代码:
// 创建对象 const person = {firstName:"Bill", lastName:"Gates"}; // 定义 getter Object.defineProperty(person, "fullName", { get: function () {return this.firstName + " " + this.lastName;} });
完整实例:
<!DOCTYPE html> <html> <body> <h1>添加 Getter 和 Setter</h1> <p id="demo"></p> <script> // 创建对象 const person = {firstName:"Bill", lastName:"Gates"}; // 定义 getter Object.defineProperty(person, "fullName", { get: function () {return this.firstName + " " + this.lastName;} }); document.getElementById("demo").innerHTML = person.fullName ; </script> </body> </html>
运行结果:
添加 Getter 和 Setter Bill Gates
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
一个计数器实例
// 定义对象 const obj = {counter:0}; // 定义 setter Object.defineProperty(obj, "reset", { get : function () {this.counter = 0;} }); Object.defineProperty(obj, "increment", { get : function () {this.counter++;} }); Object.defineProperty(obj, "decrement", { get : function () {this.counter--;} }); Object.defineProperty(obj, "add", { set : function (value) {this.counter += value;} }); Object.defineProperty(obj, "subtract", { set : function (i) {this.counter -= i;} }); // 操作计数器: obj.reset; obj.add = 5; obj.subtract = 1; obj.increment; obj.decrement;
完整实例:
<!DOCTYPE html> <html> <body> <h1>计数器实例</h1> <p id="demo"></p> <script> // 定义对象 const obj = {counter:0}; // 定义 setter Object.defineProperty(obj, "reset", { get : function () {this.counter = 0;} }); Object.defineProperty(obj, "increment", { get : function () {this.counter++;} }); Object.defineProperty(obj, "decrement", { get : function () {this.counter--;} }); Object.defineProperty(obj, "add", { set : function (value) {this.counter += value;} }); Object.defineProperty(obj, "subtract", { set : function (i) {this.counter -= i;} }); // 操作计数器: obj.reset; document.writeln('reset操作后,counter:' + obj.counter + "<br/>"); obj.add = 5; document.writeln('obj.add = 5操作后,counter:' + obj.counter + "<br/>"); obj.subtract = 1; document.writeln('obj.subtract = 1,counter:' + obj.counter + "<br/>"); obj.increment; document.writeln('obj.increment操作后,counter:' + obj.counter + "<br/>"); obj.decrement; document.writeln('obj.decrement操作后,counter:' + obj.counter + "<br/>"); </script> </body> </html>
运行结果:
计数器实例 reset操作后,counter:0 obj.add = 5操作后,counter:5 obj.subtract = 1,counter:4 obj.increment操作后,counter:5 obj.decrement操作后,counter:4
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html