项目作者: pushpendersingh1

项目描述 :
This repository contains common javascript questions
高级语言:
项目地址: git://github.com/pushpendersingh1/javascript-questions.git
创建时间: 2018-07-29T06:07:14Z
项目社区:https://github.com/pushpendersingh1/javascript-questions

开源协议:

下载


javascript-questions

1. What are Closure, Lexical Environment & Execution Context in javascript ?

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

2. What is Hoisting ? Are let & const are also hosted in JS ?

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

3. Create Private variable in Javascript.

Using Classic prototype pattern

  1. var Car = (function(){
  2. var speed = 80;
  3. function Car(model,wheels,seats){
  4. this.model = model;
  5. this.wheels = wheels;
  6. this.seats = seats;
  7. }
  8. Car.prototype.run = function(){
  9. console.log('car of model '+this.model+' having '+this.wheels+' wheels & '+this.seats+' seats is running at speed of '+speed+' km/hr');
  10. }
  11. return Car;
  12. })();
  13. var maruti = new Car('Maruti 800',4,5);
  14. maruti.run();

Using Module pattern

  1. var watch = (function(){
  2. var timer = 0;
  3. function increment(){
  4. timer++;
  5. console.log('timer value is: '+timer);
  6. }
  7. function decrement(){
  8. timer--;
  9. console.log('timer value is: '+timer);
  10. }
  11. return {
  12. increment: increment,
  13. decrement: decrement,
  14. };
  15. })();
  16. watch.increment();
  17. watch.decrement();

Using Factory pattern

  1. var CarFactory = function(model,wheels,seats){
  2. var speed = 80;
  3. function Car(model,wheels,seats){
  4. this.model = model;
  5. this.wheels = wheels;
  6. this.seats = seats;
  7. }
  8. Car.prototype.run = function(){
  9. console.log('car of model '+this.model+' having '+this.wheels+' wheels & '+this.seats+' seats is running at speed of '+speed+' km/hr');
  10. }
  11. Car.prototype.setSpeed = function(newSpeed){
  12. speed = newSpeed;
  13. }
  14. Car.prototype.getSpeed = function(){
  15. return speed;
  16. }
  17. return new Car(model,wheels,seats)
  18. };
  19. var maruti = CarFactory('800',4,4);
  20. var marcedeze = CarFactory('AMG',4,4);
  21. maruti.setSpeed(100)
  22. maruti.getSpeed() // 100
  23. marcedeze.setSpeed(120)
  24. mercedeze.getSpeed() // 120

What if we want to have variable speed to be same in all the factory objects ?

simply create speed variable outside, so it wont be part of closure which wont be part of new object.

  1. var CarFactory = (function(){
  2. var speed = 80;
  3. return function(model,wheels,seats){
  4. function Car(model,wheels,seats){
  5. this.model = model;
  6. this.wheels = wheels;
  7. this.seats = seats;
  8. }
  9. Car.prototype.run = function(){
  10. console.log('car of model '+this.model+' having '+this.wheels+' wheels & '+this.seats+' seats is running at speed of '+speed+' km/hr');
  11. }
  12. Car.prototype.setSpeed = function(newSpeed){
  13. speed = newSpeed;
  14. }
  15. Car.prototype.getSpeed = function(){
  16. return speed;
  17. }
  18. return new Car(model,wheels,seats)
  19. };
  20. })();
  21. var maruti = CarFactory('800',4,4);
  22. var marcedeze = CarFactory('AMG',4,4);
  23. maruti.setSpeed(100)
  24. maruti.getSpeed() // 100
  25. mercedeze.getSpeed() // 100