JavaScript中的继承

JavaScript中的继承,javascript,inheritance,Javascript,Inheritance,我正在读一本关于继承的JS书,我真的很困惑每一行都在做什么。有人能告诉我我的理解是否正确吗 function classA(sColor){ this.color = sColor; this.sayColor = function (){ alert(this.color); }; } function ClassB(sColor, sName){ this.newMethod = classA; //assigning a reference to a

我正在读一本关于继承的JS书,我真的很困惑每一行都在做什么。有人能告诉我我的理解是否正确吗

function classA(sColor){
   this.color = sColor;
   this.sayColor = function (){
      alert(this.color);
   };
}

function ClassB(sColor, sName){
   this.newMethod = classA; //assigning a reference to a parent class
   this.newMethod(sColor);  //calling the constructor of the parent class

   //question: so what is 'this' then? from my understanding when we call 'new ClassB('a','b')'
   //we instantiate a new object and that new object becomes the 'this' in this case. Does this
   //line mean we are converting the this to classA?

   delete this.newMethod;   //deleting reference to the CONSTRUCTOR of the parent class

   this.name = sName;
   this.sayName = function(){
      alert(this.name);
   }

}

这是一种平庸的做法

function ClassB(sColor, sName){
   classA.call(this, sColor); 

   this.name = sName;
   this.sayName = function(){
      alert(this.name);
   }
}

var f = new ClassB("green", "boy");
f.sayName(); // boy
f.sayColor(); // green
基本上,您可以使用
this
对象调用
classA
构造函数

JavaScript没有类,它只有操作对象的对象和函数。您的
ClassA
函数操作
ClassB

ClassA
只是一个操作对象的函数。在本例中,它操纵
上下文
对象,该对象是
。ClassB所做的一切就是说

  • 让ClassA操纵这个
  • 将名为
    name
    的属性添加到
    this
  • 将名为
    sayName
    的方法添加到
    this
奖金:

有一种更好的方法可以在JavaScript中实现OO

// A is a blueprint for an object with a method sayColor
var A = (function() {
  var self = Object.create({});
  self.sayColor = function() { 
    alert(this.color);
  };
  return self;
}());

// B is a blueprint for an object with methods sayColor and sayName
var B = (function() {
  // clone the methods from A
  var self = Object.create(A);
  self.sayName = function() {
    alert(this.name);
  };
  return self;
}());

// clone B and set the properties for name and color
var b = Object.create(B, {
  name: { value: "boy" },
  color: { value: "green" }
});

/* or use a factory

var bFactory = function(name, color) {
  return Object.create(B, {
    name: { value: name },
    color: { value: boy }
  });
}

b = bFactory("boy", "green");

*/

b.sayName();
b.sayColor();

使用ES5,因此使用来支持传统浏览器。

如果这是你的书教给你的继承模式,我会去买另一本书。例如,Crockford的好部分。我很少在js中实际使用继承,但理解这一点很重要。如果你的目标是了解正在发生的事情,那很酷,但如果你想了解继承,那就没有用。至少继承是像上面那样使用的。这是这本书展示的第一个继承示例,是的,我试图理解代码中每个地方的“this”是什么。哪本书?我同意Magnar的观点,好的部分(和权威指南)是你应该读的书。面向Web开发人员的专业JavaScript。我试着读了一下权威指南,发现它很快就过时了。不过我还没有读过好的部分。谁一直引用新实例化的classA实例?@Dennis除非调用
newClassA
,否则你不会创建classA的新实例。您实际做的是调用
ClassA
函数,并说使用
ClassB
中的
this
作为
ClassA中的
this
// A is a blueprint for an object with a method sayColor
var A = (function() {
  var self = Object.create({});
  self.sayColor = function() { 
    alert(this.color);
  };
  return self;
}());

// B is a blueprint for an object with methods sayColor and sayName
var B = (function() {
  // clone the methods from A
  var self = Object.create(A);
  self.sayName = function() {
    alert(this.name);
  };
  return self;
}());

// clone B and set the properties for name and color
var b = Object.create(B, {
  name: { value: "boy" },
  color: { value: "green" }
});

/* or use a factory

var bFactory = function(name, color) {
  return Object.create(B, {
    name: { value: name },
    color: { value: boy }
  });
}

b = bFactory("boy", "green");

*/

b.sayName();
b.sayColor();