• Tutorials Logic, IN
  • +91 8092939553
  • info@tutorialslogic.com

JavaScript this and new Keyword

JavaScript this Keyword

JavaScript this keyword refers to the current object. In other words, while executing every JavaScript function has a reference to its current execution context, known as this. In simple, here execution context means how the function is called. JavaScript this has different values depending on where it is used-

Global Context:-

Object's Method:-

The call(), apply() and bind() Method:-

JavaScript new Keyword

The new keyword is used to create an instances of objects from a constructor function, which has to be placed before the constructor function call and will do the following-

  • It creates a new object and the type of this object is simply object.
  • It sets the new empty objects invisible prototype property(i.e. __proto__) to be the constructor function visible and accessible prototype property.
  • It binds this keyword to the newly created object and executes the constructor function.
  • It returns the newly created object.
										    
											function TutorialsLogic (tutorial) {
												this.tutorial = tutorial;
											}
											const website = new Dog(12);
											console.log(website); //
											console.log(website.__proto__ === TutorialsLogic.prototype) // true