This is quite bizarre, no idea why it happens, but here it is.
When I do this:
/^\d+$/.test('16')
it works fine. But when I do something like the following, I get an error
var t = /^\d+$/.test;
t('16');
The error I get is this:
TypeError: Method RegExp.prototype.test called on incompatible receiver [object Window]
I don’t know what it has got to do with Window over here….any idea?
When you do
/^\d+$/.test('16')you are invoking thetestfunction with your regexp as thethisobject (i.e. as a method invocation on an object).When you run
t(16)you have no object specified, and sothisdefaults to the top object, which iswindow.To replicate the first behavior you’d have to do this: