-
Notifications
You must be signed in to change notification settings - Fork 0
Javascript
- What are closures Closures is a combination of a function with the lexical environment in which it was declared. In other words, closure gives you access to the outer function's scope from an inner function. To use closure, simply define a inner function another function and expose it by returning it. The inner function will have access to the outer function's scope , even after the outer function has returned.
function outer(){ var name = "neha"; return function(){ console.log(name); }; } var inner = outer(); inner();
Benefits of Closures
- Data Probability : we can expect all the variables to be in scope when the function was first defined and the function is also called.
- This is a way of implementing data privacy. So only the privileged function has access to the private variable of outer function.
- Prototype vs Class inheritence Class is a blueprint and inherits from another class and creates a parent, child relationship between the classes. It creates a tight coupling between the classes. Javascript's object systems are based on prototypes instead of classes. A prototype is a working object instance. Objects inherit from another object. Instances can be composed from many source objects, allowing for selective inheritance.
` //Class Inheritence
var car = function(){ var drive = function(){ console.log("I am driving"); } };
var car2 = Object.create(car.prototype, {color = blue;} `
pure functions don't modify the state of variables outside its scope. It has no side effects. Will always give the sam results, no matter what the state of the application is.
A promise is an object that will produce a single value sometime in the future. A promise will be in 3 states : success, error , pending. Promises are eager. It will start doing whatever task you want it do as soon as promise constructor is invoked. ` const wait = time => new Promise((resolve) => setTimeout(resolve, time));
wait(3000).then(() => console.log('Hello!')); // 'Hello!' `
If we have declared a variable but not defined its initial value ; we will get undefined. If not , we get "not defined"
var x; console.log(x) ;// undefined console.log(variableA) ; //not defined
In js, a private method is very ineffieienct; A copy of the method is maintained for all instances.
https://www.codementor.io/nihantanu/21-essential-javascript-tech-interview-practice-questions-answers-du107p62z https://career.guru99.com/top-85-javascript-interview-questions/ https://www.toptal.com/javascript/interview-questions
- Event Bubbling When an event happens on an element, first the handlers on it are called, then on its parents, and then on its parents and so on.