这5个方法(add,subtract...),我准备加在Number.prototype下,现在有一个问题是我怎么获取方法的调用者自身的属性,也就是例子中的数字3。
Number.prototype.add = function(num) {
return this + num;
}
Number.prototype.subtract= function(num) {
return this - num;
}
(3).add(5).subtract(3); // 5
上面是基本的例子,返回值是一个数字就可以调用在Number
的方法。获取调用者自身就通过this
, 如果是一个对象就调用.valueOf()
方法。
this
this.valueOf()