美文网首页
JS深入浅出

JS深入浅出

作者: 九又四分之三o | 来源:发表于2018-03-20 12:58 被阅读0次

    一、数据类型

    1、六种原始数据类型


    六种数据类型.png

    2、隐式转换

    • “+” 与 “-”
    “37”-7 //30
    "37" + 7 //377
    
    • “==”和"==="
      "=="
      类型相同,同===
      类型不同,尝试类型转换和比较:
      null == undefined 相等
      number == string 转 number
    1 == "1.0" //true
    

    boolean ==? 转number

    1== true //true
    

    object == number | string 尝试对象转为基本类型

    new String('hi') == 'hi' //true
    

    ===
    类型不同,返回false
    类型相同:
    undefined === undefined
    null === null
    NaN != NaN
    new Object != new Object

    • 包装类型
      number、string、boolean有包装类型
    包装类型.png

    str是string(基本类型),本身是没有方法的。当尝试把基本类型的str当做对象一样访问时,例如:str.length;
    解释器会创建一个临时的包装对象,执行了str.length之后,这个包装对象会立即销毁。
    所以,给str添加属性t并赋值能执行成功,但是执行完之后会立即销毁,所以再次访问这个属性时就显示undefined了,因为又重新创建了一个新的包装对象。

    • 类型检测
      typeof:适合基本类型和函数对象的判断。遇到null失效。
    typeof  100  //number
    typeof  true  //boolean
    typeof  Function  //function
    typeof  (undefined) //undefined
    typeof  new Object()  //object
    typeof  [1, 2]  //object
    typeof  NaN  //number
    typeof null  //object 历史原因,先记住
    

    instanceof:判断对象。原理:利用原型链。在不同iframe和window间检测时失效。

    Object.prototype.toString:判断对象类类型。遇到null和undefined失效。
    constructor:指向构造器,可被改写,使用时要小心。
    duck type:

    二、表达式、运算符

    <b>表达式</b>是一种JS短语,可使JS解释器用来产生一个值。 ---《JS权威指南》
    1、原始表达式:常量、直接量,如3.14,“test”;关键字,如null,this,true;变量,如i,k,j;
    2、初始化表达式:数组、对象的初始化表达式[1,2]、[1,,,4]、{x:1,y:2}
    3、函数表达式

    var fe = function(){};
    (function(){console.log('hello')})();
    

    4、属性访问表达式

    var o = {x:1};
    o.x
    o['x']
    

    5、调用表达式

    func();
    

    6、对象创建表达式

    new Func(1,2);
    new Object
    

    <b>运算符</b>
    1、条件运算符 -- c?a:b
    2、逗号运算符 -- a,b(少见,面试前看看)
    3、delete delete-- obj.x
    4、in -- "document" in window
    5、instanceof -- obj instanceof Func
    6、new -- new ClsName()
    7、this -- return this
    8、typeof -- typeof 100
    9、void -- void 0

    三、语句

    function foo() {
    var a = b = 1;
    }
    foo();
    console.log(typeof a);  //'undefined'
    console.log(typeof b);  //'number'
    

    其中隐式地创建了全局变量b。

    四、对象

    五、数组

    六、函数

    七、this

    八、闭包和作用域

    九、OOP

    十、正则与模式匹配

    相关文章

      网友评论

          本文标题:JS深入浅出

          本文链接:https://www.haomeiwen.com/subject/ktojtxtx.html