Mootools源码分析 — Number



/*
对数字类型的扩展实现
将很多跟数字有关的运算建立快捷方式,比如Math的所有方法
*/
Number.implement({ //限制
limit: function(min, max){
  //注意取最大值和取最小值的顺序和输入值
  return Math.min(max, Math.max(min, this));
}, /*
四啥五入,本方法是为了解决JS的浮点运算问题
先根据运算要求的精度放大,使小数点右移,使用Math.round之后再使小数点右移
所以不直接使用Math.round
*/
round: function(precision){
  precision = Math.pow(10, precision || 0);
  return Math.round(this * precision) / precision;
}, //Ruby way的方法,表示迭代次数
times: function(fn, bind){
  for (var i = 0; i < this; i++) fn.call(bind, i, this);
}, //parseFloat的快捷方式
toFloat: function(){
  return parseFloat(this);
}, //parseInt的快捷方式
toInt: function(base){
  return parseInt(this, base || 10);
}});//为times方法建立名为each的别名
Number.alias('times', 'each');//为数字类型建立到Math对象方法的快捷方式
(function(math){
var methods = {};
math.each(function(name){
  if (!Number[name]) methods[name] = function(){
  return Math[name].apply(null, [this].concat($A(arguments)));
  };
});
Number.implement(methods);
})(['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'sin', 'sqrt', 'tan']);


金鳞岂是池中物,一遇风云便化龙