• JavaScript 第七章总结


    前言

    主要介绍了关于 JavaScript 中有关 type 的问题。讲了很多关于各种 type 的 idiosyncrasies.

    谈谈JavaScript types

    在 JavaScript 中,有两种数据类型:

    • Primitive Types,包括了数值,字符串,布尔值等等
    • Objects,Objects 又包括三个部分
      1.一部分是由 JavaScript 自带的一些,比如:Math;
      2.另外一部分是 the browser 提供的,比如 document; 
      3.还有一部分是用户自己 create 的 objects.
      ##使用 typeof 来确定数据 type 
      格式 console.log(typeof 123);
      typeof 是一个 built-in operator, 可以用它来得到数据的类型。

    谈谈 undefined 这个 type

    定义:

    undefined 是一个值(value),它属于 undefined 类型的变量。
    有三种情况属于 undefined :

    • variable 的情况:这个 variable 没有进行 initialize
    • array 的情况:这个 array item 不存在
    • object 的情况: 这个 property 没有 value,或者它的 value 被 delete.

    与 null 的区别

    null 的定义:null is intended to represent an object that isn't there.
    null 的 type: object
    场合:当使用 getElementById('id') 的时候,如果不存在这个 id 代表的 element object, 将会返回一个 null 值。

    与 NaN 的区别:

    NaN 的定义: NaN 是“Not a Number" 的简写形式,它表示 value that is a value but can't be represented
    场合

    • var c = Math.sqrt(-9);
    • var b = "food" * 1000;
    • var a = 0/0 判别 NaN 的方法:由于 NaN 是唯一不等于自身的 value,所以通过 isNaN() 这个 funcition 来判断是否为 NaN.

    == 与 === 的区别

    定义

    在进行 == 的运算的时候,会进行 type conversion, 并遵守一定的 rules,但是 === 是严格意义的判断是否相等(即数据类型和值全都相等)
    == 在进行 conversion 的rules 包括两层:

    当两个 operands 's type相同:

    对它们的 value 进行比较:

    当两个 operands 数据类型不同:

    先进行类型的转换,在进行比较,有下面3种情况:

    1. string 和 number:先将 string 转换成 number,然后再将转换成的 number 与 一开始的 number 进行比较。
    2. boolean 和 any other type:现将 boolean 转换成 number, 然后在进行比较。
    3. null 和 undefined:these values both essentially represent "no value",(that is, a variable with no value, or an object with no value), so they're considered to be equal.

    错题:“true" == true; 返回值为 false.

    +,-,*,/ 时进行的 type conversion

    + 进行的 type conversion

    规则:当其中的一个 operand 的 type 为 string 类型的时候,+ 会作为一个 concatenate oprator,将另一个 type 的值转换为 value.

    其他 arithmetic operator 进行的 type conversion

    规则:当其中有 arithmetic operator 的时候,会将其中的 string 类型转换为 number 类型,然后再进行计算。 

    truthy 和 falsey 类型 的值

    定义:They aren't technically true or false, but they behave like they are.
    进行判断的 secret: concentrate on knowing what is falsey,and then everything else you can consider truthy.
    falsey 的五种类型:

    1. undefined
    2. null
    3. 0
    4. ""
    5. NaN

    关于 String

    String 既是 primitive type, 也属有 object,因此可以用于打印,也可以调用它的一些 property 和 method.

    与 string 有关的 property

    • length:得到 string 的长度
      ##与 string 有关的 method
    • indexOf('string',y),得到相关字符串第一次出现的 index 值,y 值表示从 index 为.. 开始,它是 optional 的
    • split("|"),用 delimiter 将字符串对象分开之后,返回一个 含有各个 piece 的 array
    • substring(x,y),得字符串 index 从 x 到 y (不包含 y)的值。
  • 相关阅读:
    10. Regular Expression Matching
    9. Palindrome Number (考虑负数的情况)
    8. String to Integer (整数的溢出)
    7. Reverse Integer (整数的溢出)
    LeetCode Minimum Size Subarray Sum
    LeetCode Course Schedule II
    Linux 文件缓存 (一)
    LeetCode Tries Prefix Tree
    Linux : lsof 命令
    LeetCode Binary Tree Right Side View
  • 原文地址:https://www.cnblogs.com/FBsharl/p/10226395.html
Copyright © 2020-2023  润新知