Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

keyword new js

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
//create a object with three keys, make, model, and year

var myCar = new Car('Eagle', 'Talon TSi', 1993);
// use the new operator to create any number of car objects with this template object Car above
Comment

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
Comment

new keyword in js

ObjMaker = function() {this.a = 'first';};
// ObjMaker is just a function, there's nothing special about it that makes 
// it a constructor.

ObjMaker.prototype.b = 'second';
// like all functions, ObjMaker has an accessible prototype property that 
// we can alter. I just added a property called 'b' to it. Like 
// all objects, ObjMaker also has an inaccessible [[prototype]] property
// that we can't do anything with

obj1 = new ObjMaker();
// 3 things just happened.
// A new, empty object was created called obj1.  At first obj1 was the same
// as {}. The [[prototype]] property of obj1 was then set to the current
// object value of the ObjMaker.prototype (if ObjMaker.prototype is later
// assigned a new object value, obj1's [[prototype]] will not change, but you
// can alter the properties of ObjMaker.prototype to add to both the
// prototype and [[prototype]]). The ObjMaker function was executed, with
// obj1 in place of this... so obj1.a was set to 'first'.

obj1.a;
// returns 'first'
obj1.b;
// obj1 doesn't have a property called 'b', so JavaScript checks 
// its [[prototype]]. Its [[prototype]] is the same as ObjMaker.prototype
// ObjMaker.prototype has a property called 'b' with value 'second'
// returns 'second'
Comment

new keyword in JS

It does 5 things:

It creates a new object. The type of this object is simply object.
It sets this new object's internal, inaccessible, [[prototype]] (i.e. __proto__) property to be the constructor function's external, accessible, prototype object (every function object automatically has a prototype property).
It makes the this variable point to the newly created object.
It executes the constructor function, using the newly created object whenever this is mentioned.
It returns the newly created object, unless the constructor function returns a non-null object reference. In this case, that object reference is returned instead.
Comment

new keyword in js

**Important Points** 

1.It creates a new object. The type of this object is object.
2.It sets this new object's internal, inaccessible, [[prototype]] (i.e. __proto__) property to be the constructor function's external, accessible, prototype object (every function object automatically has a prototype property).
3.It makes the this variable point to the newly created object.
4.It executes the constructor function, using the newly created object whenever this is mentioned.
5.It returns the newly created object, unless the constructor function returns a non-null object reference. In this case, that object reference is returned instead.
Note: constructor function refers to the function after the new keyword, as in

new ConstructorFunction(arg1, arg2)
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert number into string 
Javascript :: . is not recognized as an internal command npm run 
Javascript :: how to prevent previous radio button active react native 
Javascript :: foreach function into arrow with addeventlistener 
Javascript :: react datetime mannual editing 
Javascript :: js vue array change position 
Javascript :: function<asterisk in JS 
Javascript :: debounce polyfill 
Javascript :: mock js random 
Javascript :: AND Or condition in text with bracket how to divide in javascript 
Javascript :: datetimepicker 
Javascript :: mongoose $in operator order not respected 
Javascript :: elon musk 4k photo 
Javascript :: js palindrome number 
Javascript :: neo4j get first 3 nodes 
Javascript :: eachfeature leaflet 
Javascript :: jquery to javascript converter online 
Javascript :: how to clear screen in vis code 
Javascript :: attach a generated pdf to a smtpjs mail in js 
Javascript :: express get slash value 
Javascript :: generate tabuada java script 
Javascript :: give gray offlien scale to website 
Javascript :: namesandroles javascript 
Javascript :: three js buffergeometry raycasting face site:stackoverflow.com 
Javascript :: what is steal.js 
Javascript :: How can I display an image and text from an array on a webpage? Ask Question 
Javascript :: gatsby browsersync 
Javascript :: laravel datables get next input jquery next 
Javascript :: phaser game height 
Javascript :: arrow function component react shortcut vscode 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =