function as constructor | javascript

PHOTO EMBED

Fri Apr 19 2024 06:18:05 GMT+0000 (Coordinated Universal Time)

Saved by @gitjck #javascript

function myFunction(arg1, arg2) {
  const firstName = arg1;
  this.lastName  = arg2;
  this.cusfun = function (wow) {
  	console.log('---->', this.firstName, wow);
    return firstName;
  }
}

const myObj = new myFunction("John","Doe");
console.log(myObj.cusfun('hi'));
console.log('--------');
console.log(myObj.lastName);
console.log(myFunction.lastName);
console.log(myObj.firstName);
console.log(myFunction.firstName);
console.log('--------');
console.log(typeof myFunction);
console.log(typeof myObj);
content_copyCOPY

call function with 'new' keyword, works as constructor