As a newcomer to Object Oriented Programming, I'm unsure if I'm using the correct terminology. When creating "traditional objects," I typically follow this structure:
(New File: Person.js)
function Person() {
this.name;
this.getName = function getName(){
return this.name;
}
this.setName = function setName(name){
this.name = name;
}
}
To use it in the main file, I would do something like:
var myFriend = new Person();
myFriend.setName("Bob");
This requires creating an instance of the object to access its functions.
However, what I desire is akin to a "static object" found in Java or libraries. For example, with built-in Math functions, I can simply write:
Math.sin(3.14);
Without needing to instantiate an object first:
var myMath = new Math();
myMath.sin(3.14);
Ultimately, I aim for a library setup, but I'm uncertain about the process
(File: mathlib.js)
//mathlib.js
function mathlib() {
this.printStuff = function printStuff(){
alert("mathlib print");
}
}
Then in the main file:
mathlib.printStuff();
Encountering an error: TypeError: mathlib.printStuff is not a function
I'm puzzled as to where my mistake lies.. (I am including the file, just as before)