zoukankan      html  css  js  c++  java
  • JS---基础知识

    ==表示相等,即转换后值相等。

    console.log("1.23"==1.23);//true
    console.log(0==false);//true
    console.log(null==undefined);//true
    console.log(new Object()==new Object());//false
    console.log([1,2]==[1,2]);//false,对象表示一种引用,只要不是同一个对象,就不相等。

     number==string 转number  1=="1.0"//true
     boolean==? 转number 1==true//true,true转为1,false转为0
      object==number|string 尝试转为基本类型 new String('hi')=='hi'//true

    ===表示绝对相等,即类型一样,值一样。

    console.loconsole.log("1.23"===1.23);//false
    console.log(0==false);//false
    console.log(undefined===null);//false
    console.log(null===null);//true
    console.log(NaN===NaN);//false,NaN和任何值比较都不相等。

    JavaScript基本类型有number,string,boolean,null,undefined,其中number、string、boolean都有相应的包装类型,分别是Number、String、Boolean

    var str="string";
    
    var strObj=new String("string");
    
    str
    "string"
    
    strObj
    String {0: "s", 1: "t", 2: "r", 3: "i", 4: "n", 5: "g", length: 6, [[PrimitiveValue]]: "string"}
    
    //当我们把一个基本类型尝试去调用其包装类型的方法时,JS会很智能地把基本类型临时转换为包装类型对象,相当于new了一个对象,并值填进去,调用后,该临时对象就会销毁。
    var a="string";
    alert(a.length);//6
    a.t=3;
    alert(a.t)//undefine,3是临时对象的值,而此时临时对象已经销毁了。
    
    (123).toString//"123"
    相当于new 了一个String对象,并123填进去。

     类型检测:typeof     Object.prototype.toString.apply()     instanceof

    typeof 

    typeof 123 //“number”
    typeof "123" //“string”
    typeof true //“boolean”
    typeof function //“function”
    typeof undefined //“undefined”
    typeof null //“object”
    typeof NaN //“number”,NaN和Infinite一样,相当于数值类型的特殊值

     Object.prototype.toString.apply()

    //对象
    Object.prototype.toString.apply([1,2]) //"[object Array]"
    Object.prototype.toString.apply(function(){})//"[object Function]"
    Object.prototype.toString.apply(undefined)//"[object Undefined]"
    Object.prototype.toString.apply(null)//"[object Null]"
    基本类型
    Object.prototype.toString.apply("hello")//"[object String]"
    Object.prototype.toString.apply(1)//"[object Number]"
    Object.prototype.toString.apply(true)//"[object Boolean]"

    instanceof

    function Student(){}
    
    li=new Student();
    
    li instanceof Student
    //true

    小结:
    typeof 适合基本类型和函数检测,遇到null失效。  

    [[Class]] 通过{}.toString拿到,适合内置对象和基本类型,遇到undefined和null失效  

    instanceof 适合自定义对象,也可以检测原生对象。

    表达式

    //原始表达式
    3.14,"test"    //常量    
    null,this,true    //关键字
    i,j,k //变量
     //复合表达式
    10*20   
    //数组对象初始化表达式
    [1,2],{x:1,y:2}
    //函数表达式
    var fe=function(){};    
    (function(){console.log('hello');})();    
    //属性访问表达式
    var o={x:1};   
    o.x
    o['x']    
    //调用表达式
    func();
    // 对象创建表达式
    new Func(1,2);
    new Object //不带参数    
        

    运算符

    //  ?:
    var cj=85;
    var result=(cj>=60?"及格":"不及格");//及格
    
    //delete    
    var obj={x:1};
    obj.x //1
    delete obj.x    
    obj.x //undefine
    
    //in
    window.x=1;
    'x'  in window;  //true    
    
    //instanceof typeof
    {} instanceof Object;//true
    typeof 100 ==="number" //true
    
    //this
    this   //window(浏览器)
    var obj={
        func:function(){
            return this;
        }
    };    
    obj.func();  //obj

     Js中没有块级作用域

    for(var i=0;i<10;i++){
      ... 
    }
    console.log(i);  //10
    
    //相当于
    var i=0;
    for(;i<10;i++){
      ...
    }
    console.log(i);//10

    try catch finally

    try{ 
      throw "test"; //手动抛出一个异常
    }catch(ex){
      console.log(ex);
    }finally{
      console.log("end.");
    }
    
    //test
    //end.
    try{
    throw new Error("test");}
    catch(ex){
    console.error(ex.message);
    }finally{
    console.log("end.");
    }
    //test
    //end.
  • 相关阅读:
    推荐一款稳定快速免费的前端开源项目 CDN 加速服务
    MySQL限时解答
    OneProxy的功能与限制
    MySQL浮点计算存在的问题与解决方案
    DAS、SAN、NAS的区别
    气质
    受制于人
    mysqlbinlog flashback 5.6完全使用手册与原理
    Innodb引擎 compact模式下元组的磁盘存储结构
    数据迁移程序
  • 原文地址:https://www.cnblogs.com/beast-king/p/5296285.html
Copyright © 2011-2022 走看看