简体   繁体   English

找不到有效的JavaScript继承模式

[英]Can't find a JavaScript inheritance pattern that works

I know that JS inheritance has been discussed thoroughly, and I've got a couple of books on it myself, but I haven't found one that works with my current constructor pattern, and I'm not grasping the theory behind it. 我知道已经对JS继承进行了详尽的讨论,而我本人也有几本书,但是我还没有找到适合我当前构造函数模式的书,而且我没有掌握它的理论基础。

This is the object pattern I've used: 这是我使用的对象模式:

MYAPP.Animal = function() {

    function Animal() {
        this.position = {x: 0, y: 0};
    };

    Animal.prototype.getPosition = function() {
        return this.position.x;
    };

    return Animal;
}();

MYAPP.Dog = function() {

    function Dog(param) {
        this.name = param.name;
        this.position.x = param.x;
    };

    Dog.prototype.getName = function() {
        return this.name;
    };

    return Dog;
}();

var snoopy = new MYAPP.Dog({
    name:"snoopy",
    x: 10
    });

var slinky = new MYAPP.Dog({
    name:"slinky",
    x: 20
    });

I thought I'd cracked it but then it appeared as though the two dog's shared the same instance of Animal, and the first dog's position was overwritten by the second. 我以为我已经破解了它,但是随后看起来好像两只狗共享了相同的Animal实例,第一只狗的位置被第二只狗的位置覆盖。

Is inheritance possible with this approach? 用这种方法可以继承吗? Thanks in advance 提前致谢

EDIT: Looking for a vanilla JS solution 编辑:寻找香草JS解决方案

You first of all need to link Dog and Animal in some way. 您首先需要以某种方式链接“ Dog和“ Animal You could achive the inheritance you want with .call() and Object.create() 您可以使用.call()Object.create()实现所需的继承

Your modified code could then look something like this: 修改后的代码如下所示:

MYAPP.Animal = function() {

    function Animal() {
        this.position = {x: 0, y: 0};
    };

    Animal.prototype.getPosition = function() {
        return this.position.x;
    };

    return Animal;
}();

MYAPP.Dog = function() {

    function Dog(param) {
        MYAPP.Animal.call(this);
        this.name = param.name;
        this.position.x = param.x;
    };

    Dog.prototype = Object.create(MYAPP.Animal.prototype,{
        getName: {
            value: function() {
                return this.name;
            },
            enumerable: true,
            configurable: true, 
            writable: true
        }
    });

    Dog.prototype.constructor = Dog;

    return Dog;
}();

var snoopy = new MYAPP.Dog({
    name:"snoopy",
    x: 10
});

var slinky = new MYAPP.Dog({
    name:"slinky",
    x: 20
});

Also have a look at this article on developer.mozilla.org 也可以在developer.mozilla.org上查看此文章

建议您使用提供goog.inherits()功能的Google Closure库

This is a pretty broad question, with the possibility of many opinion based answers. 这是一个相当广泛的问题,可能有许多基于意见的答案。 The code posted above does not even try to inherit or extend Animal though? 上面发布的代码甚至没有尝试继承或扩展Animal

Basically it all depends on your preferences and requirements for your project. 基本上,这一切都取决于对项目的偏好和要求。 I'm guessing that you dont want to go down a framework route otherwise you would of done that already. 我猜您不想沿框架路线走,否则您已经做完了。

I would suggest looking at UnderscoreJS , it's a JS Utility library that is quite small and offers some very nice and handy functions that you'll be able to use throughout your project, particulary you may want to look at Underscore Extend . 我建议您查看UnderscoreJS ,它是一个JS Utility库,它很小,并且提供了一些非常好用的函数,您可以在整个项目中使用它们,尤其是您可能要查看Underscore Extend

Their description of what it is: 他们对它的描述:

Underscore provides over 100 functions that support both your favorite workaday functional helpers: map, filter, invoke — as well as more specialized goodies: function binding, javascript templating, creating quick indexes, deep equality testing, and so on Underscore提供了100多种功能,可同时支持您最喜欢的工作功能助手:映射,过滤器,调用—以及更专业的功能:函数绑定,javascript模板,创建快速索引,深度相等测试等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM