I see kind of a mixup of class vs instance method and the linking to module.
First module has no direct connection to class. Only what is returned in a module can be interpreted as a plain function or constructor function, and latter can be understood as class method.
In a Class based object-oriented language - which JavaScript is NOT (it's prototype based but can be 'emulated') - the class is (best) an actual (first class) object - and 'understands' only class methods, of which at least one is usually at constructer, either called a method - in ClassObject.new() - or with a language key word new ClassObject(). Latter is JavaScripts flavor and invokes the constructor with ES6 class definition or the previous style when it was just a method having things inside like this.stateProperty = aValue;. Any constructor returns an instance. With the term instance we come to the instance methods, which are only understood by instances.
In a constructor, this refers to an instance and is the context of your setup.
If you need to refer to a class method in an class or instance method, you just name the class. Since in JavaScript the constructor (function) is practically the only class method (except you attach function attributes to the constructor function), you repeat the same class or another class - latter pattern is common where as former is rarely found (except in chained / linked / tree-d type of constructs). An example of the rarer kind would be the create and return of a directory that needs a creation of a multi-level path with the option of auto-create when missing... The example has the oddity of wasting the just constructed instance when already existing. In that case it would explicitly say return foundDirectory;.
Anyway, I hope this clears up some of the muddied terminology... which is difficult to respond to. It's like reading a riddle and then with the help of some 6th sense to assume what the comment or question is all about.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I see kind of a mixup of class vs instance method and the linking to module.
First module has no direct connection to class. Only what is returned in a module can be interpreted as a plain function or constructor function, and latter can be understood as class method.
In a Class based object-oriented language - which JavaScript is NOT (it's prototype based but can be 'emulated') - the class is (best) an actual (first class) object - and 'understands' only class methods, of which at least one is usually at constructer, either called a method - in
ClassObject.new()
- or with a language key wordnew ClassObject()
. Latter is JavaScripts flavor and invokes the constructor with ES6 class definition or the previous style when it was just a method having things inside likethis.stateProperty = aValue;
. Any constructor returns an instance. With the term instance we come to theinstance methods, which are only understood by instances.
In a constructor,
this
refers to an instance and is the context of your setup.If you need to refer to a class method in an class or instance method, you just name the class. Since in JavaScript the constructor (function) is practically the only class method (except you attach function attributes to the constructor function), you repeat the same class or another class - latter pattern is common where as former is rarely found (except in chained / linked / tree-d type of constructs). An example of the rarer kind would be the create and return of a directory that needs a creation of a multi-level path with the option of auto-create when missing... The example has the oddity of wasting the just constructed instance when already existing. In that case it would explicitly say
return foundDirectory;
.Anyway, I hope this clears up some of the muddied terminology... which is difficult to respond to. It's like reading a riddle and then with the help of some 6th sense to assume what the comment or question is all about.