javaScript中的原型链返回undefined

时间:2016-12-07 11:19:41

标签: javascript

有人可以帮助解决这个问题。每当我调用 kid.say 时,控制台都会返回undefined。我期待它查找原型链,让孩子可以访问函数



function inherit(C, P) {
  var F = function() {};
  F.prototype = P.prototype;
  C.prototype = new F();
}

// the parent constructor
function Parent(name) {
  this.name = name || 'Adam';
}

// adding functionality to the prototype
Parent.prototype.say = function() {
  return this.name;
};

function inherit(C, P) {
  var F = function() {};
  F.prototype = P.prototype;
  C.prototype = new F();
}

// the parent constructor
function Parent(name) {
  this.name = name || 'Adam';
}

// adding functionality to the prototype
Parent.prototype.say = function() {
  return this.name;
};

var dad = new Parent();

// child constructor
function Child(name) {
  Parent.apply(this, arguments);
}

var kid = new Child("Patrick");

inherit(kid, dad);

console.log(kid.say);




1 个答案:

答案 0 :(得分:3)

inherit函数用于设置两个构造函数之间的继承,而不是单个对象,但kiddad是单个对象。

虽然可能(从ES2015开始)在以标准方式创建现有对象后更改现有对象的原型,但不建议这样做。< / p>

所以问题实际上只是因为你误用了inherit功能。你会想要:

inherit(Child, Parent);

...并且您希望在创建kid之前执行此操作。

(另外,无关,您已经分配到Parent.prototype.say 两次,功能定义相同。只需一次就足够了,我已经删除了下面多余的一个。)

如果你这样做,它会正确记录该功能(因为你没有调用它,你只是提到它):

&#13;
&#13;
function inherit(C, P) {
  var F = function() {};
  F.prototype = P.prototype;
  C.prototype = new F();
}

// the parent constructor
function Parent(name) {
  this.name = name || 'Adam';
}

// adding functionality to the prototype
Parent.prototype.say = function() {
  return this.name;
};

function inherit(C, P) {
  var F = function() {};
  F.prototype = P.prototype;
  C.prototype = new F();
}

// the parent constructor
function Parent(name) {
  this.name = name || 'Adam';
}

inherit(Child, Parent);

var dad = new Parent;

// child constructor
function Child(name) {
  Parent.apply(this, arguments);
}

var kid = new Child("Patrick");

console.log(kid.say);
&#13;
&#13;
&#13;

如果您想要致电,那么您需要()console.log(kid.say());

&#13;
&#13;
function inherit(C, P) {
  var F = function() {};
  F.prototype = P.prototype;
  C.prototype = new F();
}

// the parent constructor
function Parent(name) {
  this.name = name || 'Adam';
}

// adding functionality to the prototype
Parent.prototype.say = function() {
  return this.name;
};

function inherit(C, P) {
  var F = function() {};
  F.prototype = P.prototype;
  C.prototype = new F();
}

// the parent constructor
function Parent(name) {
  this.name = name || 'Adam';
}

inherit(Child, Parent);

var dad = new Parent;

// child constructor
function Child(name) {
  Parent.apply(this, arguments);
}

var kid = new Child("Patrick");

console.log(kid.say());
&#13;
&#13;
&#13;

从ES2015开始,不再需要inherit功能,只需使用class功能(如果您需要支持较旧的环境,请进行转换):

class Parent {
    constructor(name) {
        this.name = name;
    }
    say() {
        return this.name;
    }
}

class Child extends Parent {
}

let kid = new Child("Patrick");

console.log(kid.say()); // Patrick

即使在ES5(2009年12月)中, inherit版本已被Object.create淘汰:

Child.prototype = Object.create(Parent.prototype);

请注意,inherit的那个版本省略了一个重要的步骤,即修复新创建的原型对象的constructor引用。

Child.prototype.constructor = Child;