开发者

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

开发者 https://www.devze.com 2023-04-03 01:55 出处:网络
What\'s going on in this method parseFloat(\'123.234\').t开发者_运维知识库oFixed(2); How to create such functions on the results of which we can call other functions? Can someone provide the intern

What's going on in this method

parseFloat('123.234').t开发者_运维知识库oFixed(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?


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消