2

like below code:

function test()  { 
            console.log( typeof this ) 
}
test.call(3)

the code output 'object' in console. but if I remove typeof it output [Number: 3]

so why typeof this not output number???

suoyong
  • 463
  • 4
  • 20
  • 1
    It's not a duplicate answer of the stated one, we have a different behaviour here, due to the `.call()` invocation. – cнŝdk Jun 28 '17 at 09:14
  • 1
    @chsdk I don't understand, this is *exactly* what's discussed in the duplicate: `function dis() { return this }; dis.call(5)` – deceze Jun 28 '17 at 09:15
  • @deceze Yes you are right, but it was a little unclear due to the misleading title of the question and the long clarification in the accepted answer. – cнŝdk Jun 28 '17 at 09:18
  • 1
    To clarify the problem here, `typeof this` returns `object` inside your function because you are invoking it with `test.call(3);` where [**.call()**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) is converting `3` to `object` as stated in the docs: *if the method is a function in non-strict mode , null and undefined will be replaced with the global object and **primitive values will be converted to objects***. – cнŝdk Jun 28 '17 at 09:27
  • 1
    @chsdk thanks so much, I got my problem, mark you. – suoyong Jun 28 '17 at 09:32

0 Answers0