Any function in javascript can be a constructor
function A(paramA, paramB) {
this.paramA = paramA;
this.paramB = paramB;
//do something
}
A.prototype.method1 = function(){
console.log(this)
console.log('Inside method 1' + this.paramA)
}
var a = new A(1, {name: 'Name'});
console.log(a.paramA);
console.log(a.paramB.name)
a.method1()
All instance variables can be created using this.<variable-name>=<value>;.
Instance methods can be created using the prototype property of the constructor function.
You can read more about constructors
Simple “Class” Instantiation
Simple JavaScript Inheritance
You also can check whether a parameter exists using
if(paramB == undefined) {
//do something if paramB is not defined
}
typeof a == 'undefined')