Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a method from a class from another class?

Tags:

javascript

oop

I want to use Object Oriented Programming technique with JavaScript but I can't access a method from one class from another class. How can do like the following?

class one{

  write(){
    console.log("Yes! I did!");
  }
}

class two{
   var object=new one();

   tryingMethod(){
   object.write();
   }
}

I get the following error:

Uncaught SyntaxError: Unexpected identifier -->> for var object=new one();

like image 604
Hamdi Bayhan Avatar asked Aug 26 '16 23:08

Hamdi Bayhan


People also ask

Can you call methods from other classes?

You can also use the instance of the class to call the public methods of other classes from another class. For example, the method FindMax belongs to the NumberManipulator class, and you can call it from another class Test.

How do you access a method from another class in Python?

Call method from another class in a different class in Python. we can call the method of another class by using their class name and function with dot operator. then we can call method_A from class B by following way: class A: method_A(self): {} class B: method_B(self): A.

How can I access a private method from another class?

We can call the private method of a class from another class in Java (which are defined using the private access modifier in Java). We can do this by changing the runtime behavior of the class by using some predefined methods of Java. For accessing private method of different class we will use Reflection API.

How do you call a method from another class in C++?

Class B inherits from Class A, and I want class A to be able to call a function created in class B. using namespace std; class B; class A { public: void CallFunction () { B b; b. bFunction(); } }; class B: public A { public: virtual void bFunction() { //stuff done here } };


1 Answers

Your syntax is not legal. There should be an error in your console showing you which line of code is not correct.

If it's a static method (doesn't use any instance data), then declare it as a static method and you can directly call it.

If it's an instance method, then you would typically create an object of type one and then call the method on that object (usually in the constructor).

To make the method static (which appears to be fine in your specific case):

class One {
  static write(){
    console.log("Yes! I did!");
  }
}

class Two {
   tryingMethod(){
     One.write();
   }
}

For the non-static case, you don't have the proper syntax. It appears you want to create the instance of the One object in a constructor for Two like this:

class One {
  write(){
    console.log("Yes! I did!");
  }
}

class Two {
   constructor() {
       this.one = new One();
   }

   tryingMethod(){
     this.one.write();
   }
}

var x = new Two();
x.tryingMethod();

Note: I'm also following a common Javascript convention of using an identifier that starts with a capital letter for the class/constructor name such as One instead of one.

like image 71
jfriend00 Avatar answered Oct 04 '22 14:10

jfriend00