0

I am a c++ programmer, Here is a C++ code, how to have similar JS code,

class A {
 public:
  void sayHello();
};

class B  {
 public:
  A a;
};

main()
{
 B b;
 b.a.sayHello(); 
}

2 Answers 2

6
// Define class A
function A() {}
A.prototype.sayHello = function() { alert('hello!'); };

// Define class B
function B() { this.a = new A(); }

// Use them
var b = new B();
b.a.sayHello();
Sign up to request clarification or add additional context in comments.

Comments

1

The most basic and simplest example:

function A() {
  return {
    sayHello: function() {

    }
  }
}

function B() {
  return {
    a: new A()
  }
}

var b = new B();

b.a.sayHello();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.