我在javascript语言精粹P47上看到的这段代码,自己执行,检查很多遍还是报错:
Function.prototype.method = function(name, func) {
this.prototype[name] = func;
return this;
}; Number.method('integer', function() {
return Math[this < 0 ? 'ceiling' : 'floor'](this);
});
document.write((-10 / 3).integer());看看是哪里问题,求精辟解释!
Function.prototype.method = function(name, func) {
this.prototype[name] = func;
return this;
}; Number.method('integer', function() {
return Math[this < 0 ? 'ceiling' : 'floor'](this);
});
document.write((-10 / 3).integer());看看是哪里问题,求精辟解释!
ceil
返回大于等于其数字参数的最小整数。
Math.ceil(number)
必选项number 参数是数值表达式。floor 方法
应用于: Math 对象
返回小于等于其数值参数的最大整数。
Math.floor(number)
必选项 number 参数是数值表达式。原型链:JS使用prototype定义原型链,可以理解为扩展对象的方法
Function.prototype.method = function(name, func) { //为Function对象扩展method 方法
this.prototype[name] = func; return this;
};
Number.method('integer', function() { //为Number对象扩展integer方法
return Math[this < 0 ? 'ceil' : 'floor'](this);
//number的值小于0则调用Math['ceil']方法,大于0调用Math['floor']方法进行处理
});
document.write((-10 / 3).integer());