JavaScript 对象原型

所有 JavaScript 对象都从原型继承属性和方法。

在前一章里,我们学到了如何使用对象构造器

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>JavaScript 对象</h1>
  5. <p id="demo">
  6. <script>
  7. function Person(first, last, age, eye) {
  8. this.firstName = first;
  9. this.lastName = last;
  10. this.age = age;
  11. this.eyeColor = eye;
  12. }
  13. var myFriend = new Person("埃隆", "马斯克", 50, "蓝色");
  14. var myBrother = new Person("马克", "扎克伯格", 40, "绿色");
  15. document.getElementById("demo").innerHTML =
  16. "我朋友是 " + myFriend.age + "岁。 我表哥是 " + myBrother.age + "岁。";
  17. </script>
  18. </body>
  19. </html>

我们已经认识到,您无法为已有的对象构造器添加新属性:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>JavaScript 对象</h1>
  5. <p>您不能将新属性添加到构造函数。</p>
  6. <p id="demo">
  7. <script>
  8. function Person(first, last, age, eye) {
  9. this.firstName = first;
  10. this.lastName = last;
  11. this.age = age;
  12. this.eyeColor = eye;
  13. }
  14. Person.nationality = "美国";
  15. var myFriend = new Person("埃隆", "马斯克", 50, "蓝色");
  16. var myBrother = new Person("马克", "扎克伯格", 40, "绿色");
  17. document.getElementById("demo").innerHTML =
  18. "我朋友的国籍是 " + myFriend.nationality;
  19. </script>
  20. </body>
  21. </html>

如需向构造器添加一个新属性,则必须把它添加到构造器函数:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>JavaScript 对象</h1>
  5. <p id="demo">
  6. <script>
  7. function Person(first, last, age, eye) {
  8. this.firstName = first;
  9. this.lastName = last;
  10. this.age = age;
  11. this.eyeColor = eye;
  12. this.nationality = "美国";
  13. }
  14. var myFriend = new Person("埃隆", "马斯克", 50, "蓝色");
  15. var myBrother = new Person("马克", "扎克伯格", 40, "绿色");
  16. document.getElementById("demo").innerHTML =
  17. "我朋友的国籍是 " + myFriend.nationality + ". 我哥的国籍是 " + myBrother.nationality;
  18. </script>
  19. </body>
  20. </html>

原型继承

所有 JavaScript 对象都从原型继承属性和方法。

日期对象继承自 Date.prototype。数组对象继承自 Array.prototype。Person 对象继承自 Person.prototype。

Object.prototype 位于原型继承链的顶端:

日期对象、数组对象和 Person 对象都继承自 Object.prototype。


向对象添加属性和方法

有时,您希望向所有给定类型的已有对象添加新属性(或方法)。

有时,您希望向对象构造器添加新属性(或方法)。


使用 prototype 属性

JavaScript prototype 属性允许您为对象构造器添加新属性:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>JavaScript 对象</h1>
  5. <p id="demo">
  6. <script>
  7. function Person(first, last, age, eye) {
  8. this.firstName = first;
  9. this.lastName = last;
  10. this.age = age;
  11. this.eyeColor = eye;
  12. }
  13. Person.prototype.nationality = "English";
  14. var myFriend = new Person("埃隆", "马斯克", 50, "蓝色");
  15. document.getElementById("demo").innerHTML =
  16. "我朋友的国籍是 " + myFriend.nationality;
  17. </script>
  18. </body>
  19. </html>

JavaScript prototype 属性也允许您为对象构造器添加新方法:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>JavaScript 对象</h1>
  5. <p id="demo">
  6. <script>
  7. function Person(first, last, age, eye) {
  8. this.firstName = first;
  9. this.lastName = last;
  10. this.age = age;
  11. this.eyeColor = eye;
  12. }
  13. Person.prototype.name = function() {
  14. return this.firstName + " " + this.lastName
  15. };
  16. var myFriend = new Person("埃隆", "马斯克", 50, "蓝色");
  17. document.getElementById("demo").innerHTML =
  18. "我朋友是 " + myFriend.name();
  19. </script>
  20. </body>
  21. </html>

请只修改您自己的原型。绝不要修改标准 JavaScript 对象的原型。

分类导航