This repository contains common javascript questions
Closure: In JavaScript, closures are created every time a function is created, at function creation time. To use a closure, simply define a function inside another function and expose it.
Lexical Environment: In JavaScript, every running function, code block, and the script as a whole have an associated object known as the Execution Context: When your code runs in the JavaScript Engine. Each statement of your code is executed in a certain Execution Context. A new Execution Context is created whenever function invocation is there.
closures
execution-context
Hoisting: It is JavaScript’s default behavior of moving declarations to the top.
All declarations (var, let, const, function, function*, class) are “hoisted” in JavaScript.
A variable declared by let or const has a so-called temporal dead zone (TDZ): When entering its scope, it can’t be accessed (got or set) until execution reaches the declaration.
hoisting
Using Classic prototype pattern
var Car = (function(){
var speed = 80;
function Car(model,wheels,seats){
this.model = model;
this.wheels = wheels;
this.seats = seats;
}
Car.prototype.run = function(){
console.log('car of model '+this.model+' having '+this.wheels+' wheels & '+this.seats+' seats is running at speed of '+speed+' km/hr');
}
return Car;
})();
var maruti = new Car('Maruti 800',4,5);
maruti.run();
Using Module pattern
var watch = (function(){
var timer = 0;
function increment(){
timer++;
console.log('timer value is: '+timer);
}
function decrement(){
timer--;
console.log('timer value is: '+timer);
}
return {
increment: increment,
decrement: decrement,
};
})();
watch.increment();
watch.decrement();
Using Factory pattern
var CarFactory = function(model,wheels,seats){
var speed = 80;
function Car(model,wheels,seats){
this.model = model;
this.wheels = wheels;
this.seats = seats;
}
Car.prototype.run = function(){
console.log('car of model '+this.model+' having '+this.wheels+' wheels & '+this.seats+' seats is running at speed of '+speed+' km/hr');
}
Car.prototype.setSpeed = function(newSpeed){
speed = newSpeed;
}
Car.prototype.getSpeed = function(){
return speed;
}
return new Car(model,wheels,seats)
};
var maruti = CarFactory('800',4,4);
var marcedeze = CarFactory('AMG',4,4);
maruti.setSpeed(100)
maruti.getSpeed() // 100
marcedeze.setSpeed(120)
mercedeze.getSpeed() // 120
simply create speed variable outside, so it wont be part of closure which wont be part of new object.
var CarFactory = (function(){
var speed = 80;
return function(model,wheels,seats){
function Car(model,wheels,seats){
this.model = model;
this.wheels = wheels;
this.seats = seats;
}
Car.prototype.run = function(){
console.log('car of model '+this.model+' having '+this.wheels+' wheels & '+this.seats+' seats is running at speed of '+speed+' km/hr');
}
Car.prototype.setSpeed = function(newSpeed){
speed = newSpeed;
}
Car.prototype.getSpeed = function(){
return speed;
}
return new Car(model,wheels,seats)
};
})();
var maruti = CarFactory('800',4,4);
var marcedeze = CarFactory('AMG',4,4);
maruti.setSpeed(100)
maruti.getSpeed() // 100
mercedeze.getSpeed() // 100