- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript: lexical scoping issue using new keyword constructor while adding inner function?
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
- Related Articles
- Lexical Scoping in Dart Programming
- Creating JavaScript constructor using the “new” operator?
- How to invoke a JavaScript Function with 'new' Function Constructor?
- How to define a JavaScript function using Function() Constructor?
- Authentication options in SAP HANA while adding new system
- Using the new keyword in C#
- Block Scoping in JavaScript.
- Creating a JavaScript array with new keyword.
- What is function() constructor in JavaScript?
- Accessing variables in a constructor function using a prototype method with JavaScript?
- What is the 'new' keyword in JavaScript?
- Number of carries required while adding two numbers in JavaScript
- New keyword in Java
- What is lexical this in JavaScript?
- How to invoke a function with a function constructor in JavaScript?

Advertisements