专栏首页nummy《eloquent javascript》 notes1

《eloquent javascript》 notes1

value, types and operator

There is only one value in JavaScript that is not equal to itself, and that is NaN, which stands for “not a number”.

console.log(NaN == NaN)
// → false

When something that doesn’t map to a number in an obvious way (such as "five" or undefined) is converted to a number, the value NaN is produced. Further arithmetic operations on NaN keep producing NaN.

When comparing values of the same type using ==, the outcome is easy to predict: you should get true when both values are the same, except in the case of NaN.But when the types differ, JavaScript uses a complicated and confusing set of rules to determine what to do. In most cases, it just tries to convert one of the values to the other value’s type. However, when null or undefined occurs on either side of the operator, it produces true only if both sides are one of null or undefined.

console.log(null == undefined);
// → true
console.log(null == 0);
// → false

That last piece of behavior is often useful. When you want to test whether a value has a real value instead of null or undefined, you can simply compare it to null with the == (or !=) operator.

The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string ("") count as false, while all the other values count as true. Because of this, expressions like 0 == false and "" == false are also true.

data strcuture

Almost all JavaScript values have properties. The exceptions are null and undefined. If you try to access a property on one of these nonvalues, you get an error.

null.length;
// → TypeError: Cannot read property 'length' of null

the document object

Unlike methods such as getElementsByTagName, the object returned by querySelectorAll is not live. It won’t change when you change the document.

The querySelector method (without the All part) works in a similar way. This one is useful if you want a specific, single element. It will return only the first matching element or null if no elements match.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 外观模式

    上面的例子中WalkingDrone类隐藏了Leg类的复杂同步操作,提供了一个更为简单的接口。

    用户2936342
  • Python Context Managers

    Sometimes, there is a need to execute some operations between another pair of op...

    用户2936342
  • 享元模式

    在上面的例子中,使用Link类来保存链接数据,Browser使用这些链接,可能存在很多链接引用同一张图片,如果每个图片都保存下来,会很占用内存。所以这里使用享元...

    用户2936342
  • 2017年12月编程语言排行榜

    The programming languages Kotlin and C seem to be the only candidates to become ...

    程序员的酒和故事
  • pyspark-ml学习笔记:逻辑回归、GBDT、xgboost参数介绍

    逻辑回归、GBDT可以参考pyspark开发文档:http://spark.apache.org/docs/latest/api/python/pyspark....

    MachineLP
  • 嘈杂的中型量子计算机上的串扰的软件缓解(CS ET)

    串扰是嘈杂的中级量子(NISQ)系统中的主要噪声源,并且是硬件设计的基本挑战。当多个指令并行执行时,指令之间的串扰会破坏量子状态并导致错误的程序执行。我们的目标...

    非过度曝光
  • 协作机器人的数字孪生:案例研究(CS CY)

    人机协作(HRC)可以在传统上难以实现自动化的领域(例如装配)中提高自动化水平。但是,对适应性和人类存在动力的需求使人机协作系统的全部潜力难以实现。 本文探讨了...

    小童
  • tensorflow Image解码函数

    Decode a PNG-encoded image to a uint8 tensor. 将一个png编码的图像解码成一个uint8张量。The attr c...

    DrawSky
  • hdu-----(3746)Cyclic Nacklace(kmp)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32...

    Gxjun
  • JVM 问题排查和性能优化常用的 JDK 工具

    JDK 提供了一系列用于监控、诊断 Java 进程的工具,它们在 JDK 安装目录的 bin 目录下,有 jps、jcmd、jstack、jinfo、jmap ...

    古时的风筝

扫码关注云+社区

领取腾讯云代金券