(0).add(2)
就是给Number
对象添加一个add
方法
Number.prototype.add = function(val){
return this + val;
}
这样哦:
Number.prototype.add = function(n) {
return this + n;
};
var result = (1).add(2);
console.log(result);//结果为:3
(1)
将原始类型1
自动装箱,成了Number(1)
,然后为Number
的prototype
上提供新的add
方法。
结果就是两数相加哦!