Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

New keyword in js

// New keyword do two things 
// 1.) Make an empty object
// 2.) return this
// 3.) assign the function prototype to proto

// Example 

function createUser(firstName , age) {
  this.firstName = firstName;
  this.age = age;
}

createUser.prototype.about = function(){
  console.log(`Name of user is ${this.firstName} age of user is ${this.age}`);
}

const user1 = new createUser("Fahad", 23);

// user1.about();
// So if we're defining a function that'll call with new keyword then we make the function and assign capital letter to fast alphabet to tell other developers that it'll call with new keyword otherwise it'll not gonna work...

// EXAMLE
function Hello(firstName) {
  this.firstName = firstName;
}

Hello.prototype.hello = function (){
  console.log("Hello", this.firstName);
}

const helloTofahad = new Hello("Fahad", 34);

console.log(helloTofahad.hello())

// That's how it'll work
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #New #keyword #js
ADD COMMENT
Topic
Name
1+7 =