D
D
Dmitry Petrov2016-08-03 14:43:05
JavaScript
Dmitry Petrov, 2016-08-03 14:43:05

What is the correct way to create classes inside objects in javascript?

Hi all. I want to understand how to correctly implement the similarity of OOP in JavaScript. For example, I want to create an object like this:
var man = new Earth.Man(prop1, prop2);
How can I create a constructor inside the Earth object (if I understand this entry correctly)?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2016-08-03
Protko @Fesor

implement a kind of OOP in JavaScript

Where in the abbreviation "OOP" do you see the word "class"?)
What for? "namespaces"? This is not needed if there are modules.
any function can be used as a constructor.

Y
yociyavi, 2016-08-03
@yociyavi

In js, the same function is both a class and a constructor. Check out:
https://learn.javascript.ru/classes

N
napa3um, 2016-08-03
@napa3um

Better instead of constructors built into the main class of subordinate objects (if their logic is really subordinate to the main class, and not just using it as a namespace) use factories in the style:
var man = Earth.createMan(prop1, prop2);

V
Vitaly, 2016-08-03
@vshvydky

class NAME extends SuperName {
    constructor(params){
      super();
      /// ....
     }
    methodName(params){ 
     ////.....
    }
}

Naturally, you need to understand that this will not work everywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question