⏱ 0:05est. 3 min
Chapter 59- Dangerous Aside ('new' and Functions)
Notes
Example 1 :
//We use first letter as the capital letter in case of function constructor
function Person(firstname, lastname){ //Constructor
console.log(this); // Person {}
this.firstname =firstname;
this.lastname =lastname;
}
person.prototype.getFullName = function (){
return this.firstname + ‘ ' + this.lastname;
}
//If we don't use new keyword, It will still execute the function but it returns undefined
var john = Person(‘John', ‘Doe');
//so following line of code with throw an error, as we are accessing prototype of undefined
console.log(john.getFullName);
//So always use new keyword to access prototype properties