js

  1. null and undefied (PJ3.4.3)

you should never explicitly set the value of a variable to undefined, but the same does not hold true for null. Any time an object is expected but is not available, null should be used in its place.

Another thing, Number(null) returns 0 while Number(undefied) returns NaN.

  1. use "or" operator ("||") to set a default value, use "and" operator ("&&") to check before using. e.g.:
var middle = stooge["middle-name"] || "(none)";
flight.equipement && flight.equipement.model;
  1. isNaN
isNaN("true") //true, Boolean to number
isNaN("10")   //true, string "10" to 10

Although typically not done, isNaN() can be applied to objects. In that case, the
object’s valueOf() method is fi rst called to determine if the returned value can
be converted into a number. If not, the toString() method is called and its
returned value is tested as well. This is the general way that built-in functions
and operators work in ECMAScript

  1. Number() vs parseInt()
Number(null)   //0
parseInt(null)  //NaN

parseInt("070") 

how strange, PJ says:

In ECMAScript 3 JavaScript engines, the value “070” is treated as an octal literal and becomes the decimal value 56. In ECMAScript 5 JavaScript engines, the ability to parse octal values has been removed from parseInt() , so the leading zero is considered invalid and the value is treated the same as “0”, resulting in the decimal value 0. This is true even when running ECMAScript 5 in non-strict mode.

but I tried on my chrome and and firefox, parseInt("070") just returns 70 instead of 56. So I'd better always specify the base...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容