coure2011
coure2011

Reputation: 42464

what's going on in parseFloat('number').toFixed(2)?

What's going on in this method

parseFloat('123.234').toFixed(2);

How to create such functions on the results of which we can call other functions? Can someone provide the internal structure of such methods? Is this method chaining?

Upvotes: 4

Views: 4003

Answers (1)

KooiInc
KooiInc

Reputation: 122936

This indeed is method chaining. parseFloat returns a Number object, which has a method toFixed.

This is a basic example to show you how it works:

function Construct(){
   this.method1 = function(){
     return this;
   };
   this.method2 = function(){
     alert('called method2');
     return this;
   };
   this.method3 = function(){
     alert('method3: I am not chainable');
   };
}

var instance = new Construct;
instance.method1().method2().method3(); 
  //=> alerts 'called method2' and 'method3: I am not chainable'

Upvotes: 4

Related Questions