Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange "getter" behaviour in IE9 when accessing property of `Number.prototype` from a number literal

Object.defineProperty(Number.prototype, 'foo', {
  get: function () { return this }
})

console.log(10.5.foo)
console.log(10..foo)   // 0 in IE9!
console.log(10.0.foo)  // 0 in IE9!
console.log(10.01.foo)
console.log((10).foo)  // 0 in IE9!
var x = 10
console.log(x.foo)     // 0 in IE9!

Can anyone explain this behaviour and/or suggest a workaround?

jsfiddle.net/yr7hQ/

like image 656
davidchambers Avatar asked Oct 21 '11 20:10

davidchambers


1 Answers

I can't explain that result, but you also asked for a workaround.

Demo: http://jsfiddle.net/ThinkingStiff/FJ7Qx/

Script:

Number.prototype.foo = function() { return Number( this ) };

console.log(10.5.foo())
console.log(10..foo())
console.log(10.0.foo())
console.log(10.01.foo())
console.log((10).foo())
var x = 10
console.log(x.foo())
like image 189
ThinkingStiff Avatar answered Oct 01 '22 03:10

ThinkingStiff