Posts

Showing posts from February, 2017

Inheritance in Javascript (ES6)

INHERITANCE : The  extends  keyword is used in  class declarations  or  class expressions  to create a class which is a child of another class. class ChildClass extends ParentClass { ... } The  extends  keyword can be used to subclass custom classes as well as built-in objects. The  .prototype  of the extension must be an  Object  or  null . Examples : -  Using  extends -  Please click on this link   https://googlechrome.github.io/samples/classes-es6/index.html  for a better example. Using  extends  with built-in objects -  class myDate extends Date { constructor ( ) { super ( ) ; } getFormattedDate ( ) { var months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] ; return this . getDate ( ) + '-' + months [ this . getMonth ( ) ] + '-' + th

Constructor in Javascript ES6

CONSTRUCTOR : constructor([arguments]) { ... } The  constructor  method is a special method for creating and initializing an object created with a  class .  There can only be one special method with the name "constructor" in a class. A Syntax error will be thrown, if the class contains more than one occurrence of a constructor  method. A constructor can use the  super  keyword to call the constructor of a parent class. If you don't specify a constructor method, a default constructor is used. Example : -  Please click on this link   https://googlechrome.github.io/samples/classes-es6/index.html  for a better example.

Static methods in Javascript ES6

Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions. Calling static methods  1. From another static method : In order to call a static method within another static method of the same class, you can use the  this  keyword. class StaticMethodCall { static staticMethod ( ) { return 'Static method has been called' ; } static anotherStaticMethod ( ) { return this . staticMethod ( ) + ' from another static method' ; } } StaticMethodCall . staticMethod ( ) ; // 'Static method has been called' StaticMethodCall . anotherStaticMethod ( ) ; // 'Static method has been called from another static method' 2.  From class constructor and other methods :  Static methods are not directly accessible using the  this  keyword from non-static methods. You need to call them using the class name:  CLASSNAME.STATIC

How can you secure your HTTP cookies against XSS attacks?

XSS occurs when the attacker injects executable JavaScript code into the HTML response. To mitigate these attacks, you have to set flags on the  set-cookie HTTP header: HttpOnly  - this attribute is used to help prevent attacks such as cross-site scripting since it does not allow the cookie to be accessed via JavaScript. secure  - this attribute tells the browser to only send the cookie if the request is being sent over HTTPS. So it would look something like this:  Set-Cookie: sid=<cookie-value>; HttpOnly . If you are using Express, with  express-cookie session , it is working by default.

What is the difference between sessionstorage, localstorage and Cookies?

sessionStorage, localStorage and Cookies all are used to store data on the client side. Each one has its own storage and expiration limit. localStorage : stores data with no expiration date, and gets cleared only through JavaScript, or clearing the Browser Cache / Locally Stored Data or manually deletion. sessionStorage : similar to localStorage but expires when the browser closed (not the tab). Cookie : stores data that has to be sent back to the server with subsequent requests. Its expiration varies based on the type and the expiration duration can be set from either server-side or client-side (normally from server-side). Cookies are primarily for  server-side  reading (can also be read on client-side), localStorage and sessionStorage can only be read on  client-side.

Why is node.js not suitable for heavy CPU apps?

Node is, despite its asynchronous event model, by nature single threaded. When you launch a Node process, you are running a single process with a single thread on a single core. So your  code  will not be executed in parallel, only I/O operations are parallel because they are executed asynchronous. As such, long running CPU tasks will block the whole server and are usually a bad idea. Given that you just start a Node process like that, it is possible to have multiple Node processes running in parallel though. That way you could still benefit from your multithreading architecture, although a single Node process does not. You would just need to have some load balancer in front that distributes requests along all your Node processes. Another option would be to have the CPU work in separate processes and make Node interact with those instead of doing the work itself.