js:继承

js这门傻逼的语言真让人头大!

如何实现js的继承?


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

function Child(age) {
    this.age = age;
    this.getAge = function () {
        return this.age;
    }
}

Child.prototype = new Base("base"); // 实现继承

var ch = new Child(16);

console.log("age = " + ch.getAge() + " , name = " + ch.getName());
// 输出: age = 16 , name = base

也不解释了,反正我也解释不好。

你可能感兴趣的:(大猫学javascript)