So, I have some questions about Objects in JavaScript. The main question I have is about how do Object Constructors work?
I know that they are functions, and that they take advantage of the this
keyword.
What I am confused about is below:
This is a simple Object Constructor:
function LulzImObj() {
this.rofl = "true";
this.lmao = "maybe";
}
This is a simple Function:
function lmaoIdoDis( dank ) {
if( dank ){
return "such wow";
}else{
return "rofl. no wow";
}
}
So, this is what I am confused about (would this behave as a constructor or a function with a return or both)?:
function suchWowObj( dank ) {
this.rofl = "true";
this.lmao = "maybe";
if( dank ){
this.pepe = function() { return "trulz"; };
return "such wow";
}else{
return "rofl. no wow";
}
}
context:
var herp = suchWowObj( true );
var derp = new suchWowObj( true );
var snerp = suchWowObj( false );
var werp = new suchWowObj( false );