last day (14 days later) » 

14:19
2
A: Javascript true/false function

Andrea Casacciavar magicVar = Math.sin; From here on magicVar is a reference to the Math.sin function, which is actually an Object (see Function on MDN) The Function constructor creates a new Function object. In JavaScript every function is actually a Function object. magicVar == true; // it returns f...

I have to think about it. Anyway generally speaking keep in mind that not all javascript design choices could be optimal. The language has been developed in a very short time and certainly has its flaws. I suggest you this read on the subject books.google.ie/books/about/…
@rji89 when you are satisfied with one of the answers, please don't forget to accept one meta.stackexchange.com/questions/5234/…
You are oversimplifying magicVar == true;. There are indeed objects that are equal to a boolean, just try out true == {valueOf(){return 1;}}. That table on MDN is (was) simply wrong.
@Bergi that is not an object, it's a code block. If you don't trust MDN look at the standard ecma-international.org/ecma-262/6.0/index.html 7.2.12 Abstract Equality Comparison, the relevant case is point 12. Is the standard wrong aswell?
@AndreaCasaccia: I used a method shorthand (ES6), you might need to try {valueOf: function(){return 1;}}. No, the standard is not wrong (I know it well), the table on MDN was (before I fixed it). The relevant points are ES6 §7.2.12 steps 8 and 9.
@Bergi I think got your point, you are right. I'll edit my answer to be more precise.
@Bergi anyway I wouldn't say that the table is wrong, it covers the Number, String an Boolean cases. The Object case should be considered only when your Object is not a Number, String or Boolean. My citation was oversimplifying indeed.
@Bergi No, now I finally understand, the table is actually wrong, If an object is not typeof(Number), but behaves as a Number, the behaviour will not be consistent with that table.
@Bergi take a look at my edit, I hope I got your point. Sorry for the attitude on my first comment but I put quite some effort in writing the answer and I thought it was correct. Feel free to edit if you like.
14:19
Thanks for your effort :-) Unfortunately it's still not quite right, as step 9 is applied for Math.sin before it gets to 12. So magicVar == true => magicVar == 1 => Number(magicVar) == 1 => Number(magicVar.toString()) == 1 => Number("function sin() { [native code] }") == 1 => NaN == 1 => false
Yes, there is much more going on than one might think on the first look :-)
Definetely
First step magicVar == true => magicVar == 1 is clear to me
but why magicVar == 1 => Number(magicVar) == 1 ?
6. and 7. doesn't apply
since typeof(magicVar) != String
== is applied recursively. In magicVar == 1, you're now comparing an object to a number, which takes us to step 11
Ah, yeah, I've been wrong, that step 11 takes us to
toPrimitive(magicVar) == 1 => magicVar.toString() == 1 => "function sin() { [native code] }" == 1 => Number("function sin() { [native code] }") == 1 => NaN == 1 => false
I've prematurely wrapped it in Number() as I anticipated the outcome of ToPrimitive :-)
Good, I'll think about it a little bit more then I'll update the answer hopefully for the final version
thanks, I learned something new
That's been the goal :-)
bye

last day (14 days later) »