[英]Return number's value from method on Number.prototype?
Before going in to the details of the issue I'm already fully aware of modifying built in's prototypes. 在深入探讨问题之前,我已经完全意识到要修改内置的原型。 This code is written in typescript.
这段代码是用打字稿写的。
Ok so I'm running in to an issue when I'm trying to return a number's value from a method on Number.prototype. 好的,当我尝试从Number.prototype上的方法返回数字值时,我遇到了一个问题。
Here is the definition on Number.prototype: 这是Number.prototype的定义:
Object.defineProperty(Number.prototype, 'tap', {
value: function numberTap (fn: (t: number) => void): number {
fn.call(null, this.valueOf());
return this.valueOf();
}
});
and the test I'm running: 和我正在运行的测试:
describe('Number.prototype.tap((value:number) => void): number', () => {
it('is defined', function () {
expect(Number.prototype.tap).to.be.a('function');
});
it('returns the same value as its entry point in the method chain', () => {
expect((1).tap(n => 5)).to.equal(1);
});
});
It doesn't equal 1
but [Number 1]
. 它不等于
1
但等于[Number 1]
。 If I call [Number 1].valueOf()
then it returns 1
. 如果我调用
[Number 1].valueOf()
则返回1
。 I'm already calling this.valueOf()
when returning from tap. 从水龙头返回时,我已经在调用
this.valueOf()
。 Anyone know what's going on here. 谁都知道这是怎么回事。
I figured it out. 我想到了。 I was missing a require that defined Number.prototype.tap and had Object.prototype.tap already defined so it was causing this issue.
我缺少要求定义的Number.prototype.tap并已定义Object.prototype.tap的要求,因此导致了此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.