Posts

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" }

compare password and sensitive data's in nodejs

previously I did like this to compare the sensitive data's function validatePassword(fromDbPassword, inputPassword){          if( fromDbPassword === inputPassword) return true;           return false; } There is an attack security attack called timing attack . Hackers try to crack the encryption algorithm using the way.  So How to compare password hackers cant use the timing attack V8,  JavaScript engine used by Node.js, tries to optimize the code you run from a performance point of view. It starts comparing the strings character by character, and once a mismatch is found, it stops the comparison operation. So the longer the attacker has right from the password, the more time it takes. function checkApiKey (apiKeyFromDb, apiKeyReceived) {  return cryptiles.fixedTimeComparison(apiKeyFromDb, apiKeyReceived)  } To solve this issue, you can use the npm module c...

Docker tutorials

Docker  Pull the image sudo docker pull ubuntu sudo docker run ubuntu apt-get install -y ping Then get the container id using this command: sudo docker ps -l Commit changes to the container: sudo docker commit   iman/ping  Expose the port  docker run -p 9200:9200 elasticsearch  Then run the container: sudo docker run iman/ping ping  www.google.com Ref https://stackoverflow.com/questions/19585028/i-lose-my-data-when-the-container-exits https://runnable.com/docker/binding-docker-ports

Responsive ui

To create responsive ui Following steps To set view port Html 5 tag This one sets width is device width size use media queries in css3 @media only screen and (max-width: 768px) {     /* For mobile phones: */      .header{        width : 200px;      } } Mobile orientaion view : Potrait / Landscape @media only screen and (orientation: landscape) {     body {         background-color: lightblue;     } }