Quản lý đối tượng JavaScript
ECMAScript 5 (2009) đã thêm các phương thức quản lý đối tượng này vào JavaScript:
// Adding or changing an object property
Object.defineProperty(object, property, descriptor)
// Adding or changing object properties
Object.defineProperties(object, descriptors)
// Accessing a Property
Object.getOwnPropertyDescriptor(object, property)
// Accessing Properties
Object.getOwnPropertyDescriptors(object)
// Returns all properties as an array
Object.getOwnPropertyNames(object)
// Accessing the prototype
Object.getPrototypeOf(object)
Đối tượng JavaScript.defineProperty()
Phương thức Object.defineProperty()
có thể được sử dụng để:
- Thêm thuộc tính mới vào một đối tượng
- Thay đổi giá trị thuộc tính
- Thay đổi siêu dữ liệu thuộc tính
- Thay đổi getters và setters đối tượng
Cú pháp:
Object.defineProperty( object, property, descriptor )
Thêm một thuộc tính mới
Ví dụ này thêm một thuộc tính mới vào một đối tượng:
Ví dụ
// Create an Object:
const person = {
firstName: "John",
lastName :
"Doe",
language : "EN"
};
// Add a Property
Object.defineProperty(person, "year",
{value:"2008"});
Hãy tự mình thử »Thay đổi giá trị thuộc tính
Ví dụ này thay đổi giá trị thuộc tính:
Ví dụ
// Create an Object:
const person = {
firstName: "John",
lastName : "Doe",
language : "EN"
};
// Change a Property
Object.defineProperty(person, "language",
{value : "NO"});
Hãy tự mình thử »Thay đổi siêu dữ liệu
Dữ liệu meta thuộc tính sau có thể được thay đổi:
writable : true // Property value can be changed
enumerable : true // Property can be enumerated
configurable : true // Property can be reconfigured
writable : false // Property value can not be changed
enumerable : false // Property can be not enumerated
configurable : false // Property can be not reconfigured
Getters và setters cũng có thể được thay đổi:
// Defining a getter
get: function() { return language }
// Defining a setter
set: function(value) { language = value }
Ví dụ này làm cho ngôn ngữ chỉ đọc:
Object.defineProperty(person, "language", {writable:false});
Ví dụ này làm cho ngôn ngữ không thể đếm được:
Object.defineProperty(person, "language", {enumerable:false});
JavaScript getOwnProperyNames()
Phương thức Object.getOwnProperyNames()
có thể:
- Liệt kê thuộc tính đối tượng
Cú pháp
Object.getOwnProperyNames( object )
Liệt kê tất cả các thuộc tính đối tượng
Ví dụ này lấy tất cả các thuộc tính của một đối tượng:
Ví dụ
// Create an Object
const person = {
firstName: "John",
lastName : "Doe",
language : "EN"
};
// Get all Properties
Object.getOwnPropertyNames(person);
Hãy tự mình thử » Object.getOwnProperyNames()
cũng sẽ liệt kê các thuộc tính không thể đếm được:
Ví dụ
// Create an Object
const person = {
firstName: "John",
lastName : "Doe",
language : "EN"
};
// Set the language Property not enumerable
Object.defineProperty(person, "language", {enumerable:false});
// Get all Properties
Object.getOwnPropertyNames(person);
Hãy tự mình thử »Đối tượng JavaScript.keys()
Phương thức Object.keys()
có thể:
- Liệt kê các thuộc tính đối tượng có thể đếm được
Cú pháp
Object.keys( object )
Liệt kê các thuộc tính đối tượng có thể đếm được
Ví dụ này sử dụng Object.keys()
thay thế cho Object.getOwnProperyNames()
:
Ví dụ
// Create an Object
const person = {
firstName: "John",
lastName : "Doe",
language : "EN"
};
// Change the "language" Property
Object.defineProperty(person, "language", {enumerable:false});
// Get all Enumerable Properties
Object.keys(person);
Hãy tự mình thử »Ghi chú
Phương thức getOwnPropertyNames() trả về tất cả các thuộc tính.
Phương thức Object.keys() trả về tất cả các thuộc tính có thể đếm được.
Nếu bạn xác định các thuộc tính đối tượng mà không có enumerable:false thì hai phương thức sẽ trả về giống nhau.
Thêm Getters và Setters
Phương thức Object.defineProperty()
cũng có thể được sử dụng để thêm Getters và Setters:
Ví dụ
//Create an object
const person = {firstName:"John", lastName:"Doe"};
// Define a getter
Object.defineProperty(person, "fullName", {
get:
function () {return this.firstName + " " + this.lastName;}
});
Hãy tự mình thử »Một ví dụ về bộ đếm
Ví dụ
// Define object
const obj = {counter:0};
// Define setters
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;}
});
// Play with the counter:
obj.reset;
obj.add = 5;
obj.subtract = 1;
obj.increment;
obj.decrement;
Hãy tự mình thử »