admin管理员组

文章数量:1194107

How do I define nested class in Java Script.

Here is the code snippet I have:

objA = new TestA();

function TestB ()
{
   this.testPrint = function ()
  {
     print ( " Inside testPrint " );
  }
}

function TestA ()
{
   var myObjB = new TestB();
}

Now I am trying to access testPrint using objA

objA.myObjB.testPrint();

But its giving error "objA has no properties"

How can I access testB method using objA handler?

How do I define nested class in Java Script.

Here is the code snippet I have:

objA = new TestA();

function TestB ()
{
   this.testPrint = function ()
  {
     print ( " Inside testPrint " );
  }
}

function TestA ()
{
   var myObjB = new TestB();
}

Now I am trying to access testPrint using objA

objA.myObjB.testPrint();

But its giving error "objA has no properties"

How can I access testB method using objA handler?

Share Improve this question edited Oct 9, 2008 at 2:30 jjnguy 139k53 gold badges297 silver badges326 bronze badges asked Oct 9, 2008 at 2:18 JD912864JD912864
Add a comment  | 

6 Answers 6

Reset to default 11

If you want the prototype definition of the inner nested classes to be not accessible from outside the outer class, as well as a cleaner OO implementation, take a look at this.

var BobsGarage = BobsGarage || {}; // namespace

/**
 * BobsGarage.Car
 * @constructor
 * @returns {BobsGarage.Car}
 */
BobsGarage.Car = function() {

    /**
     * Engine
     * @constructor
     * @returns {Engine}
     */
    var Engine = function() {
        // definition of an engine
    };

    Engine.prototype.constructor = Engine;
    Engine.prototype.start = function() {
        console.log('start engine');
    };

    /**
     * Tank
     * @constructor
     * @returns {Tank}
     */
    var Tank = function() {
        // definition of a tank
    };

    Tank.prototype.constructor = Tank;
    Tank.prototype.fill = function() {
        console.log('fill tank');
    };

    this.engine = new Engine();
    this.tank = new Tank();
};

BobsGarage.Car.prototype.constructor = BobsGarage.Car;

/**
 * BobsGarage.Ferrari
 * Derived from BobsGarage.Car
 * @constructor
 * @returns {BobsGarage.Ferrari}
 */
BobsGarage.Ferrari = function() {
    BobsGarage.Car.call(this);
};
BobsGarage.Ferrari.prototype = Object.create(BobsGarage.Car.prototype);
BobsGarage.Ferrari.prototype.constructor = BobsGarage.Ferrari;
BobsGarage.Ferrari.prototype.speedUp = function() {
    console.log('speed up');
};

// Test it on the road

var car = new BobsGarage.Car();
car.tank.fill();
car.engine.start();

var ferrari = new BobsGarage.Ferrari();
ferrari.tank.fill();
ferrari.engine.start();
ferrari.speedUp();

// var engine = new Engine(); // ReferenceError

console.log(ferrari);

This way you can have prototype inheritance and nested classes so that classes defined within BobsGarage.Car are not accessible outside the constructor of BobsGarage.Car but instances of them are accessible to derived classes, as shown in the test code.

Note: I am referring to the concept of Class in Javascript as defined on the MDN.

use this.myObjB instead of var myObjB

Object literals:

var objA = {
    myObjB: {
       testPrint: function(){
          print("Inside test print");
       }
    }
};

objA.myObjB.testPrint();

If you're trying to do inheritance then you may want to consider the prototype keyword.

var myObjB = function(){
    this.testPrint = function () {
       print ( " Inside testPrint " );
    }
}

var myObjA = new myObjB();
myObjA.prototype = {
   var1 : "hello world",
   test : function(){
      this.testPrint(this.var1);
   }
}

(i hope that made sense)

Your definition of TestA does not expose any public members. To expose myObjB, you need to make it public:

function TestA() {
    this.myObjB = new TestB();
}
var objA = new TestA();
var objB = objA.myObjB;

this is another way to do that.

class A {
  constructor(classB) {
    this.ObjB = classB
  }
}

class B {
  Hello() {
    console.log("Hello from class B")
  }
}

let objA = new A(new B())

objA.ObjB.Hello()

本文标签: Javascript nested classStack Overflow