最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Inheritance with Lo-Dash - Stack Overflow

programmeradmin2浏览0评论

I am trying to emulate inheritance in javascript using a the Lo-Dash javascript library.

I am simply using _.extend to do so:

function Parent() {
  var self = this;

  self.hello = function () {
    console.log('Hello from parent');
  };
}

function Child() {
  var self = this;

  _.extend(self, Parent);
}

Child.hello(); // Doesn't exist

I thought this would work since all javascript functions are objects but obviously I am wrong. Why doesn't this work and how would I properly emulate inheritance using Lo-Dash?

I am trying to emulate inheritance in javascript using a the Lo-Dash javascript library.

I am simply using _.extend to do so:

function Parent() {
  var self = this;

  self.hello = function () {
    console.log('Hello from parent');
  };
}

function Child() {
  var self = this;

  _.extend(self, Parent);
}

Child.hello(); // Doesn't exist

I thought this would work since all javascript functions are objects but obviously I am wrong. Why doesn't this work and how would I properly emulate inheritance using Lo-Dash?

Share Improve this question asked Aug 5, 2014 at 23:03 Damir BojanDamir Bojan 1072 silver badges8 bronze badges 2
  • That happens because function Child doesn't have hello property. See How to “properly” create a custom object in JavaScript? – zerkms Commented Aug 5, 2014 at 23:05
  • Even if you did it correctly you would not really inherit but just copy members of one onto another (more like mix in) for a detailed answer about JavaScript inheritance you can view this answer. stackoverflow.com/questions/16063394/… – HMR Commented Aug 6, 2014 at 7:04
Add a comment  | 

3 Answers 3

Reset to default 13

See _.create documentation for a working example.

Parent is simply the constructor for the Parent class, it does not itself have the hello property that it adds to self. You could just change the _.extend line to this: _.extend(self, new Parent()) to solve it. This works because the object returned by new Parent() does have a hello property that _.extend can copy over to self.

To access the hello property you will also have to create an instance of the Child class rather than accessing hello on the constructor. After making the above change, (new Child()).hello() should work because you accessing the hello property on the Child instance not the constructor.

But, this seems to me to be a poor solution because new Child() instanceof Parent will return false. If you want to properly set up the prototype chain so there is "true" inheritance going on you should read about psuedoclassical and prototypal inheritance.

You could use _.create() function and prototype to emulate inheritance.

function Parent() {}

Parent.prototype.hello = function() {
    console.log('Hello from parent');
};

function Child() {}

Child.prototype = _.create(Parent.prototype, {
    'constructor': Child
});

var child = new Child();
child.hello(); //Hello from parent
发布评论

评论列表(0)

  1. 暂无评论