一、原型继承
所有JavaScript对象都从原型继承属性和方法。日期对象继承自 Date.prototype。数组对象继承自 Array.prototype。Person 对象继承自Person.prototype。Object.prototype位于原型继承链的顶端。日期对象、数组对象和 Person 对象都继承自 Object.prototype。
1.JavaScript prototype属性允许您为对象构造器添加新属性:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
2.JavaScript prototype属性也允许您为对象构造器添加新方法:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
请只修改您自己的原型。绝不要修改标准JavaScript对象的原型。
二、JavaScript ES5对象方法
1.ES5 新的对象方法
(1)添加或更改对象属性:Object.defineProperty(object, property, descriptor)
(2)添加或更改多个对象属性:Object.defineProperties(object, descriptors)
(3)访问属性:Object.getOwnPropertyDescriptor(object, property)
(4)以数组返回所有属性:Object.getOwnPropertyNames(object)
(5)以数组返回所有可枚举的属性:Object.keys(object)
(6)访问原型:Object.getPrototypeOf(object)
(7)阻止向对象添加属性:Object.preventExtensions(object)
(8)如果可将属性添加到对象,则返回 true:Object.isExtensible(object)
(9)防止更改对象属性(而不是值):Object.seal(object)
(10)如果对象被密封,则返回 true:Object.isSealed(object)
(11)防止对对象进行任何更改:Object.freeze(object)
(12)如果对象被冻结,则返回 true:Object.isFrozen(object)
2.案例
(1)更改元数据:
①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 }
此例使语言为只读:
Object.defineProperty(person, "language", {writable:false});
此例使语言不可枚举:
Object.defineProperty(person, "language", {enumerable:false});
(2)列出所有属性
①此例列出对象的所有属性:
var person = {
firstName: "Bill",
lastName : "Gates"
language : "EN"
};
Object.defineProperty(person, "language", {enumerable:false});
Object.getOwnPropertyNames(person); // 返回属性数组
②列出可枚举的属性
此例只列出对象的所有可枚举属性:
var person = {
firstName: "Bill",
lastName : "Gates"
language : "EN"
};
Object.defineProperty(person, "language", {enumerable:false});
Object.keys(person); // 返回可枚举属性的数组
(3)添加属性
①此例向对象添加新属性:
// 创建对象
var person = {
firstName: "Bill",
lastName : "Gates",
language : "EN"
};
// 添加属性
Object.defineProperty(person, "year", {value:"2008"});
②添加Getter和Setter
Object.defineProperty() 方法也可以用于添加 Getter 和 Setter:
// 创建对象
var person = {firstName:"Bill", lastName:"Gates"};
// 定义 getter
Object.defineProperty(person, "fullName", {
get : function () {return this.firstName + " " + this.lastName;}
});
(4)一个计数器实例
// 定义对象
var obj = {counter:0};
// 定义 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;}
});
// 操作计数器:
obj.reset;
obj.add = 5;
obj.subtract = 1;
obj.increment;
obj.decrement;