I am writing a JS library, and there are two more things that have been in my head for quite some time.
Consider the option:
var option = { bool : true } ;
Now imagine what I'm doing either
if(option.bool) or if(option.bool === true)
In the first case, despite the fact that he is sure that he is true or false , I think this is the equivalent:
var tmp = options.bool; if ( tmp !== undefined && tmp !== null && ( tmp === true || tmp ... )
This means that to test options.bool = true, it must check both undefined and non-null before testing true.
Therefore, the latter case should be more effective. However, the latter case takes significantly more characters and will lead to an increase in lib if repeated many times.
But maybe my understanding is wrong.
Right now, even if I do:
var bool = window.t ? true : false; if ( bool === true )
Maybe the last case can be optimized by the compiler, but when it is a global parameter , I think it is different?
Please share with me your thoughts on this.
javascript firefox google-chrome
momomo Sep 16 '15 at 17:54 2015-09-16 17:54
source share