Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

method chaining in javascript


class Arithmetic {
  constructor() {
    this.value = 0;
  }
  sum(...args) {
    this.value = args.reduce((sum, current) => sum + current, 0);
    return this;
  }
  add(value) {
    this.value = this.value + value;
    return this;
  }
  subtract(value) {
    this.value = this.value - value;
    return this;
  }
  average(...args) {
    this.value = args.length
      ? (this.sum(...args).value) / args.length
      : undefined;
    return this;
  }
}

a = new Arithmetic()
a.sum(1, 3, 6)   // => { value: 10 } 
  .subtract(3)   // => { value: 7 }
  .add(4)        // => { value: 11 }
  .value         // => 11 

// Console Output
// 11
Comment

prototype chain in javascript

var o = {
  a: 2,
  m: function() {
    return this.a + 1;
  }
};

console.log(o.m()); // 3
// When calling o.m in this case, 'this' refers to o

var p = Object.create(o);
// p is an object that inherits from o

p.a = 4; // creates a property 'a' on p
console.log(p.m()); // 5
// when p.m is called, 'this' refers to p.
// So when p inherits the function m of o, 
// 'this.a' means p.a, the property 'a' of p


Comment

chaining prototype methods

function Shape(){//from  ww  w.j a  v a  2  s .  co  m
   this.isDrawable = true;
}
Shape.prototype.getDrawable = function(){
   return this.isDrawable;
};

function Rectangle(){
   this.hasFourEdges = false;
}

//inherit from Shape
Rectangle.prototype = new Shape();

Rectangle.prototype.getFourEdges = function (){
   return this.hasFourEdges;
};

var instance = new Rectangle();
console.log(instance.getDrawable());   //true
Comment

prototype chain in javascript

{
    prop: "some value",
    __proto__: {
        foo: "bar",
        constructor: ƒ doSomething(),
        __proto__: {
            constructor: ƒ Object(),
            hasOwnProperty: ƒ hasOwnProperty(),
            isPrototypeOf: ƒ isPrototypeOf(),
            propertyIsEnumerable: ƒ propertyIsEnumerable(),
            toLocaleString: ƒ toLocaleString(),
            toString: ƒ toString(),
            valueOf: ƒ valueOf()
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript Access String Characters 
Javascript :: JavaScript Precision Problems 
Javascript :: javascript Symbol Properties 
Javascript :: javascript Arrow Function as an Expressio 
Javascript :: sign changely api 
Javascript :: javascript remaining elements of an array to a variable using the spread syntax 
Javascript :: intervals 
Javascript :: javascript variable name arguments and eval are not allowed 
Javascript :: JavaScript Iterables 
Javascript :: react linkify 
Javascript :: how to write like query in node js 
Javascript :: nodjs : Stream for big file 
Javascript :: a tag 
Javascript :: javascript copy by reference 
Javascript :: flip image on y axis phaser 
Javascript :: phaser place on circles 
Javascript :: phaser enable pixel art 
Javascript :: add multiple phone using js 
Javascript :: remove text in div JQuery zqqqqqqqqqqqqqqqzzzzzzzzzzzzzzzzzzzzzzzzzzzzzqqqqqqqqqqqqqqqqqq 
Javascript :: ray intersection js 
Javascript :: condition rendering using if-else 
Javascript :: nodelist example 
Javascript :: js convert string to number 
Javascript :: json validate 
Javascript :: javascript interview questions 
Javascript :: filter bootstrap 
Javascript :: event.target 
Javascript :: postgres json 
Javascript :: javascript sort object by value descending 
Javascript :: js max number in array mdn 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =