A
Alex Shabanov
Curious javascript sample.
Consider the following code:
var s = "asd"
var f = function() { return typeof(this) }
An expression typeof(s) returns true (as it is expected), but
f.call(s) returns false!
To make matters worse consider the following snippet:
var s = "asd"
var f = function() { return this instanceof String }
An expression f.call(s) returns true, but (s instanceof String)
returns false!
The question is why call transforms this to an object? Is it made for
unification, e.g. to make this iterable using 'for (var i in this)'
loop?
It is also unclear why string literal in fact is not an instance of
String in ("asd" instanceof String) expression but it does a string
in call/apply method invokations.
I see this might be a kind of boxing/unboxing in .NET, but I see no
reason why language designers made such a choice.
Consider the following code:
var s = "asd"
var f = function() { return typeof(this) }
An expression typeof(s) returns true (as it is expected), but
f.call(s) returns false!
To make matters worse consider the following snippet:
var s = "asd"
var f = function() { return this instanceof String }
An expression f.call(s) returns true, but (s instanceof String)
returns false!
The question is why call transforms this to an object? Is it made for
unification, e.g. to make this iterable using 'for (var i in this)'
loop?
It is also unclear why string literal in fact is not an instance of
String in ("asd" instanceof String) expression but it does a string
in call/apply method invokations.
I see this might be a kind of boxing/unboxing in .NET, but I see no
reason why language designers made such a choice.