JavaScript class subclass

I want to create a subclass of B that inherits from superclass A. my code is here:

function A(){
    this.x = 1;
}

B.prototype = new A;
function B(){
    A.call(this);
    this.y = 2;
}
b = new B;
Console.log(b.x + " " + b.y );

when it starts, it shows that B is undefined.

+5
source share
4 answers

You must define constructor function B before trying to access its prototype:

function A(){
  this.x = 1;
}

function B(){
  A.call(this);
  this.y = 2;
}

B.prototype = new A;

b = new B;
console.log(b.x + " " + b.y );  // outputs "1 2"
+17
source
B.prototype = new A;
function B(){
    A.call(this);
    this.y = 2;
}

it should be

function B(){
    A.call(this);
    this.y = 2;
}
B.prototype = new A;
+7
source

Lynda.com B .

function B() {
   A.call(this);
   this.y = 2;
}

B.prototype = new A;
B.prototype.constructor = B;
+5

, (B.prototype = new A). , , , . , , .

! , (B.prototype = new A), !! , !!! !!!! , . , .

, . B.prototype = new A B.prototype = Object.create(A.prototype). , 09. 09

protoProxy = function(myClass)
{
  function foo(){};
  foo.prototype = myClass.prototype;
  return new foo();
 }

Object.create. B.prototype = new A B.prototype = protoProxy (A) 09;

+1

All Articles