有人可以解释一下继承如何在javascript(JS)中工作吗,
我已经看过很多教程,但是我不明白它正在实现什么。
即使日夜都没有结果。

我经历过的是:

console.log('Initialized..')

function Animal() {
    this.species = "animal";
}

Animal.prototype.category = function (){
    alert(this.species);
}

function Dog(){
    this.Animal();
}

copyPrototype(Dog, Animal)
var d = new Dog();
d.category();


http://www.sitepoint.com/javascript-inheritance/中的参考教程

最佳答案

在我看来,http://www.letscodejavascript.com/v3/episodes/lessons_learned/12是理解经典的OOP在JavaScript中工作时理解我们滥用的机制的最佳资源。

以下是当有人告诉我必须在JS中使用OOP时的操作。这可能是我在某处在线购买的东西。无论如何,这里是这样:

/// Make this script available somewhere
var extendsClass = this.extendsClass || function (d, b) {
    function __inheritedProto() { this.constructor = d; }
    __inheritedProto.prototype = b.prototype;
    d.prototype = new __inheritedProto();
}

var Animal = (function() {
    function Animal(data) {
        this.value = data;
    }
    Animal.prototype.method = function() {
        return this.value;
    };
    return Animal;
})();

var Dog = (function(_super) {
    extendsClass(Dog, _super);
    function Dog() {
        _super.apply(this, arguments);
    }
    Dog.prototype.method2 = function() {
        return this.value * 2; //do something else
    };
    return Dog;
})(Animal);

/// Create some instances
var animal = new Animal(1);
var dog = new Dog(2);


/// Call some methods
animal.method();  // 1
dog.method(); // 2
dog.method2();// 4

09-06 19:42