javascript - Return number's value from method on Number.prototype? -
before going in details of issue i'm aware of modifying built in's prototypes. code written in typescript.
ok i'm running in issue when i'm trying return number's value method on number.prototype.
here definition on number.prototype:
object.defineproperty(number.prototype, 'tap', { value: function numbertap (fn: (t: number) => void): number { fn.call(null, this.valueof()); return this.valueof(); } });
and 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 same value entry point in method chain', () => { expect((1).tap(n => 5)).to.equal(1); }); });
it doesn't equal 1
[number 1]
. if call [number 1].valueof()
returns 1
. i'm calling this.valueof()
when returning tap. know what's going on here.
just tried following:
object.defineproperty(number.prototype, 'tap', { value: function numbertap(fn) { fn.call(null, this.valueof()); return this.valueof(); } }); typeof (1).tap(function(){})
and runs fine.
Comments
Post a Comment