在parseFloat('number')中发生了什么.toFixed(2)?

时间:2011-09-06 08:51:06

标签: javascript

这种方法发生了什么

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

如何在我们可以调用其他函数的结果上创建这样的函数?有人能提供这种方法的内部结构吗?这种方法是否链接?

1 个答案:

答案 0 :(得分:4)

这确实是方法链。 parseFloat返回Number object,其中包含方法toFixed

这是向您展示其工作原理的基本示例:

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'