小编Joh*_*ohn的帖子

如何从Javascript中的构造函数继承构造函数?

所以我正在学习Javascript及其所有'原型优点,我对以下内容感到难过:

说我有这个

var Animal = function (a, b, c, d, e, f, g, h, i, j, k , l, m, n){
   this.a = a;
   this.b = b;
   //...etc...
};

var x = new Animal(1,2,3....);
Run Code Online (Sandbox Code Playgroud)

现在我如何创建一个继承自Animal构造函数的Cat构造函数,这样我就不必再次输入超长参数了?

换句话说,我不想这样做:

var Cat = function (a, b, c, d, e, f, g, h, i, j, k , l, m, n){
   this.a = a;
   this.b = b;
   //...etc...
};

// inherit functions if any
Cat.prototype = new Animal;

var y = new Cat(1,2,3....);
Run Code Online (Sandbox Code Playgroud)

提前致谢!Ĵ

javascript inheritance constructor

8
推荐指数
1
解决办法
992
查看次数

切换对象的原型

我试图理解原型是如何工作的.为什么下面会破坏?

var A = function A(){this.a = 0},
    aa = new A;

A.prototype = {hi:"hello"};

aa.constructor.prototype //->{hi:"hello"} ok so far :)

aa.hi //undefined?? why? :(
Run Code Online (Sandbox Code Playgroud)

javascript

4
推荐指数
1
解决办法
1341
查看次数

标签 统计

javascript ×2

constructor ×1

inheritance ×1