Vous êtes sur la page 1sur 3

Closures:

● A closure is a combination of function bundled together with the references to its


lexical environment (i.e. scope and state of environment within which it is defined).
● In other words, function defined within closure has access to three scope chains: its
own scope, scope of outer function within which it is defined and global scope.
● A closure is created everytime a function is created at function creation time.
● In order to use closure, simply define a function within another function and expose it.
By exposing a function, we mean either return it or pass it as parameter to another
function.
● An example of closure,

$(document).ready(function(){
var a = 10;

$(‘#button’).click(function(){
console.log(a); //Since a is referenced from inner function, it will not be
garbage collected even when outer function has completed execution and will be accessible
from within this function.
});
});

Prototype:

● Function’s prototype:
○ A function’s prototype is an object instance (empty initially) that will become
the prototype for all objects created using this function as constructor.
● Object’s prototype:
○ An object prototype is the object instance from which the object is inherited.
● Inhertance:
○ Inheritance can be achieved in javascript using prototypical inheritance.
○ Let us take an example,

Suppose there is a base class called “Person” which has properties defined
within constructor and methods defined on prototype object.

function Person(name, dob, gender) {


this.name = “”;
this.dob = “”;
this.gender = “”;
}

Person.prototype.getAge = function() {
//Calculate age based on value of this.dob and set it to local variable
“age”
return age;
}

Now, I want to create a class called Teacher which will inherit all properties
and methods from Person class and have additional properties and methods.

Derived class can inherit own properties of base class by calling constructor
of base class from constructor of child class passing child class context to it, i.e.

function Teacher(name, dob, gender, subject) {


Person.call(this, name, dob, gender);
this.subject = subject;
}

We also need to inherit properties and functions defined in parent class


prototype into child class (we need to inherit entire prototype chain in order to ensure
all properties and methods of parent class are completely inherited in child class). We
can achieve this by setting prototype object of class to prototype object of parent
class as follows,

Teacher.prototype = Object.create(Person.prototype);

This will set prototype object of child class to be equal to that of parent class.
However, this will also set “Teacher.prototype.constructor” property to “Person” which
we do not want. We want this to be set to “Teacher”. And this can be fixed as follows,

Teacher.prototype.constructor = Teacher;

Once this is done, we can define additional properties / functions on prototype object
of Teacher class.

Hoisting:

● Hoisting in javascript is a mechanism where variable and function declarations are


moved to the top of their scope before execution.
● Only declarations are hoisted and assignment/initialization are left in place.
● As per MDN documents:
○ For example, hoisting teaches that variable and function declarations are
physically moved to the top of your coding, but this is not what happens at all.
What does happen is that variable and function declarations are put into memory
during the ​compile​ phase, but stays exactly where you typed it in your coding.
Use strict:

● The purpose of “use strict” is to indicate that code is to be executed in strict mode.
● When strict mode is enabled,
○ You cannot use undeclared variables.
○ Eliminates javascript silent errors by changing them to throw errors.
○ It restricts use of keywords / syntax likely to be defined in next version of
ecmascript.
● Benefits of strict mode:
○ Strict mode changes previously accepted “bad syntax” into real errors.
○ In JavaScript mistyping a variable name creates a new global variable. In strict
mode, this will throw an error, making it impossible to accidentally create a
property of window variable.
○ In normal JavaScript, a developer will not receive any error feedback assigning
values to non-writable properties. And also any assignment to a non-writable
property, a getter-only property, a non-existing property, a non-existing variable,
or a non-existing object, will throw an error.

Event Driven Programming:

● Event driven programming is programming paradigm where event emitter emits


events and event handler is executed on trigger of event.
● When it comes to javascript, it is event loop which calls event handler function after
an event is triggered. It is not necessary that event handler function is executed
immediately after event has occurred. How it works is, whenever an event is
triggered, event handler function is pushed to event queue. Event loop keeps track of
both event queue and javascript stack and passes event handler function from event
queue to javascript stack when stack becomes empty.
● Event emitter in node.js:

let eventInstance = require(“events”);

let eventEmitter = eventInstance.EventEmitter();

function connectToSomething() {
//Code for connecting to something
eventEmitter.emit(“connected_to_something”);
}

eventEmitter.on(“connected_to_something”, function() {
console.log(“Established connection!”);
});

Asynchronous Programming:

Event Loop:

Node.js interview questions and good explanation on basics:


http://www.techbeamers.com/top-30-node-js-interview-questions-answers/

Vous aimerez peut-être aussi