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'
精彩评论