Posts

Showing posts from October, 2017

when to use prototypes

* For object creation optimization Built-in prototype object is if you'll be duplicating an object multiple times that will share common functionality. By attaching methods to the prototype, you can save on duplicating methods being created per each new instance. But when you attach a method to the prototype, all instances will have access to those methods * use prototypes if we need to declare a "non-static" method of the object. var myObject = function () { }; myObject.prototype.getA = function (){   alert("A"); }; myObject.getB = function (){   alert("B"); }; myObject.getB();  // This works fine myObject.getA();  // Error!

Inheritance in Javascript

Inheritance in Javascript Inheritance is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation. function Person(name, age, gender, interests) {   this.name = name   this.age = age;   this.gender = gender;   this.interests = interests;   this.getName = function() {     return this.name;   } }; Person.prototype.getAge = function() {   return this.age; } /** Teacher inherit properties from the person class. Person.call(this, name, age, gender, interests ) after .call() properties are inherited in teacher class. */ function Teacher(name, age, gender, interests, subject) {   Person.call(this, name, age, gender, interests)   // Vehicle.call(this)   this.subject = subject;   this.getSubject = function() {     return this.subject;   }   this.getName = function(...

Timer unref ref in nodejs

Timers Unref, Ref in nodejs If there is no other activity keeping the event loop running, the process may exit before the Timeout object's callback is invoked when Calling timeout.unref() var timeout = setTimeout(function() { console.log('setTimeout') }, 10000); var interval = setInterval(function(){ console.log("exectute every 10 secs") }, 5000) After unref application will stop when other than this setInterval, nothing is running in eventloop interval.unref(); If I not ref again application will stop because other than this setInterval nothing is running in eventloop interval.ref(); /** * References : https://www.bennadel.com/blog/3301-calling-timeout-unref-with-settimeout-does-not-appear-to-be-a-best-practice-in-node-js.htm * References : https://nodejs.org/api/timers.html */

Npm install

Npm install npm install --save express npm install --save-dev mocha Package.json will be "dependencies" : { "express" : "^2.2.33" }, "devDependencies" : { "mocha" : "^2.83.0" }