Was it helpful?

Question

JavaScript: lexical scoping issue using new keyword constructor while adding inner function?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

To fix this, use the concept of this keyword. User another variable to hold the value of object, to use that inside the inner function.

Example

Following is the code −

function Employee() {
   this.technologyName = "JavaScript";
   var currentTechnologyName = this;
   function workingTechnology() {
      console.log("I am working with " + currentTechnologyName.technologyName + " Technology");
   }
   workingTechnology();
}
var currentTechnology = new Employee();

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo216.js.

The output is as follows −

PS C:\Users\Amit\JavaScript-code> node demo216.js
I am working with JavaScript Technology
raja
Published on 03-Oct-2020 17:16:06
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top