×


js (ES6,ES7,ES8),OOPS js,Node.js

ABOUT JAVASCRIPT

JavaScript  is an interpreted language.A program such as C++ or Java needs to be compiled before it is run. The source code is passed through a program called a compiler, which translates it into bytecode that the machine understands and can execute. In the case of javascript  an interpreter  reads over the JavaScript code, interprets each line and runs it.Google’s V8 increases performance by compiling JavaScript to native machine code before executing it, versus executing bytecode or interpreting it.

ADVANCD JAVASCRIPT (ES6)
Here, we look at some advanced javascript concepts which might come useful while facing an interview for javascript developer

HOISTING IN JAVASCRIPT

Hoisting is JavaScript’s default behavior of moving declarations to the top of the current scope

 

Consider the above code.The function and var declarations automatically gets hoisted towards top . So the above code works fine.

VARIABLE SCOPE OF JAVASCRIPT

var,let,const,function,class,import are ways of declaring a variable
Both let and const create variables that are block-scoped –ie, they only exist within the innermost block that wraps them.(Blocks are what exists inside curley braces and the nested curley braces)
The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.In other words we can say that the var keyword is function scoped

 

Shadowing outer declaration

Even if the let variable is defined as same as var variable globally, the let variable will not be added to the global window object.
Once a const variable has been created, it can be changed only via a loop

 

TEMPORAL DEAD ZONE(TDZ)

Accessing a let or const variable before its declaration (within its scope) causes a ReferenceError. The time span when that happens, between the creation of a variable’s binding and its declaration, is called the temporal dead zone.var doesnt have temporal dead zone

 

IMMEDIATELY INVOKED FUNCTION EXPRESSIONS

 

REFERENCE ERROR AND TYPE ERROR

A ReferenceError occurs when you try to use a variable that doesn’t exist at all.
A TypeError occurs when the variable exists, but the operation you’re trying to perform is not appropriate for the type of value it contains. In the case where the detailed message says “is not defined”, this can occur if you have a variable whose value is the special undefined value

STRICT MODE

The purpose of “use strict” is to indicate that the code should be executed in “strict mode”.
With strict mode, you can not, for example, use undeclared variables.

BUBBLING

When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.
The method for it is event.stopPropagation() stops bubbling
The first method event bubbling directs the event to its intended target,

CAPTURE

The second is called event capture in which the event goes down to the element.
close() method is used to close the current window.

DATATYPES IN JAVASCRIPT

Boolean. Null. Undefined. Number. String. Symbol,Object

PROTOTYPES IN JAVASCRIPT

All JavaScript objects inherit properties and methods from a prototype.

We cannot add a new property to an object directly.wecan add it to its prototype

CALLBACKS IN JAVASCRIPT

 

MODULES IN JAVASCRIPT (FROM ES6)

We create a new modul in javascript and export in the following way.Lets say we have a file module.js.

with the following code

To use this module in another file main.js we have to require the file as in ES5.

In ES6

We import this in our main.js file in the following way

ES6 is finalized, but not fully supported by browsers To use ES6, get a compiler like Babel

Classes in ES6

ES6 introduces a keyword class which helps us write classes easly

Actually es6 classes are mostly syntactic sugar. over the javascripts prototype based in inheritance and makes it easy  to code in an oo way

CLOUSURE
A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables. It makes it possible for a function to have “private” variables. A closure is a function having access to the parent scope, even after the parent function has closed.

Classes and Clousers can be used as approaches for data encapsulation

SYMBOLS IN JAVASCRIPT

Symbols are used to create private properties.But symbol do not guarantee true privacy but can be used to separate public and internal properties of objects.In fact symbol properties are not private . Symbols are collision-free

PROMISES IS ES6

Promises help us to do asynchronous operations.Create new promise in the following way

Promises are consumed using .then when we need value of it

 

ARROW  FUNCTIONS IN ES6

Arrow functions make ‘this’ behave properly.this will have the same value as in the context of the function—it won’t mutate. The mutation typically happens each time you create a closure. For example

We get the  this inside click only by assigning it to a  variable in the scope.Arrow functions prevents this mutation and makes this available inside For example

DEFAULT PARAMETERS ES6

 

TEMPLATE LITERALS ES6

GENERATOR FUNCTIONS

Geneartor functions can be paused.The  yield keyword is used to pause and resume a generator function.Once a generator has yield-paused itself, it cannot resume on its own. An external control must be used to restart the generator.When we call the generator function it gives an instance of itself .To execute it we have to call the next() method.

The yield keyword pauses generator function execution and the value of the expression following the yield keyword is returned to the generator’s caller.

ASYNC/AWAIT(ES2017)

Async functions are essentially a better way to work with asynchronous code in JavaScript.Async functions return a Promise.The following is the proper way to create an async function

Async functions can make use of the await expression. This will pause the async function and wait for the Promise to resolve before  moving on.

Instead of using long promise chain we can make use of the async/await mechanism.This concept is introduced in the latest latest ES7

 

PROXIES ES6

The Proxy object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).

Subject can be a function,variable or an object.The interface is an object where a series of actions are defined, according to a predefined type: set, get etc.The predefined actions are called traps.

REFLECT

Reflect is a built-in object that provides methods for interceptable JavaScript operations. The methods are the same as those of proxy. Reflect is not a function object, so it’s not constructible.

DESTRUCTURING ASSIGNMENT

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

EXPONENTIATION OPERATOR (ES7)

ARRAY INCLUDES(ES7)

It check weather array includes a value

DECORATORS (ES7)

In its simplest form, a decorator is simply a way of wrapping one piece of code with another — literally “decorating” it. Decorator is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.  Decorator example

 

SHARED MEMORY AND ATOMIC(ES8)

Shared memory: we can allow multiple threads read and write the same data with the new SharedArrayBuffer constructor.

Atomics: We can use the Atomics object to make sure nothing that is being written or read will be interrupted in the middle of the process. So the operations are finished before a the next one starts.

NODE.JS

Node.js is a web application framework built on Google Chrome’s JavaScript Engine(V8 Engine).npm is a package manager that helps install node packages.It provides Command line utility to install packages,manage versions.. etc.Node.js comes with runtime environment on which a Javascript based script can be interpreted and executed.Node.js also provides a rich library of various modules .

Node.js is asynchronous non-blocking .Server never waits for an api to return data,It movies on.Some of the modules which provides API’s for various operations in node are

HTTP – To use the HTTP server

fs – The fs module provides an API for interacting with the file system

Crypto – The crypto module provides cryptographic functionality

Async Hooks – Provides an API to register callbacks tracking the lifetime of asynchronous resources created inside a Node.js application.

There are various nodejs we application development frame works like Koa,Express which helps us create an architecture for our web application.

Buffer class is a global class and can be accessed in application without importing buffer module. A Buffer is a kind of an array of integers and corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.

Piping is a mechanism to connect output of one stream to another stream. It is normally used to get data from one stream and to pass output of that stream to another stream. There is no limit on piping operations. Consider the above example, where we’ve read test.txt using readerStream and write test1.txt using writerStream. Now we’ll use the piping to simplify our operation or reading from one file and writing to another file.

Stream

Streaming  simply means a sequence of data being moved from one point to the other over time. When you have a huge amount of data to process, but you don’t need to wait for all the data to be available before you start processing it. Basically, this big data is broken down and sent in chunks

A nodejs stream is an abstract interface for working with streaming data in Node.js. The stream module provides a base API that makes it easy to build objects that implement the stream interface.

There are many stream objects provided by Node.js. For instance, a request to an HTTP server and process.stdout are both stream instances.

Streams can be readable, writable, or both. All streams are instances of EventEmitter.

The stream module can be accessed using:

There are four fundamental stream types within Node.js:

Many of the built-in modules in Node implement the streaming interface.HTTP request makes use of writable streams and http response make use of readable streams.

BUFFER

The Buffer class was introduced as part of the Node.js API to make it possible to manipulate or interact with streams of binary data.A Buffer is a kind of an array of integers and corresponds to a raw memory allocation outside the V8 heap

ERROR FIRST CALLBACK
Error-first callbacks are used to pass errors and data. You have to pass the error as the first parameter, and it has to be checked to see if something went wrong. Additional arguments are used to pass data.

STUB

Stubs are functions/programs that simulate the behaviors of components/modules.

REPL

Node.js comes with virtual environment called REPL (aka Node shell). REPL stands for Read-Eval-Print-Loop. It is a quick and easy way to test simple Node.js/JavaScript code.

 

 

One Comment

  • kevn tong May 31, 2018 at 9:01 pm

    good

Leave a Reply

Your email address will not be published. Required fields are marked *