JS基础总结(一)

  • js基础

  • JS输出

document.write():该语句仅仅是向页面输出写的内容,如果页面已经加载完毕,此时使用该语句会导致页面被覆盖;

//1、下面的代码不会覆盖整个页面
<body>
    <p>这是我的测试段落</p>
    <script>
        document.write(Math.random())
    </script>
</body>

//2、下面的代码会覆盖整个页面
<body>
    <p>这是我的测试段落</p>
    <button onclick="clickMe()"></botton>
    <script>
        function clickMe(){

        document.write(Math.random())
     }
    </script>
</body>
  • JS的数据类型

  • 9种数据类型

  1. Boolean
  2. Symbol(ES6新增类型)
  3. null
  4. undefiend
  5. String
  6. Number
  7. Array
  8. Object
  9. Function
  • 检测数据类型

         用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上

  1. typeof操作符
    typeof "John" ;                // 返回 string 
    typeof 3.14   ;                // 返回 number
    typeof NaN     ;               // 返回 number
    typeof false  ;                // 返回 boolean
    typeof [1,2,3,4]   ;           // 返回 object
    typeof {name:'John', age:34};  // 返回 object
    typeof new Date();             // 返回 object
    typeof function () {};         // 返回 function
    typeof myCar;                  // 返回 undefined (如果 myCar 没有声明)
    typeof null;                   // 返回 object
        //简单示例
        <script type="text/javascript">
            var a = function test (){
    
            };
            var b = {a:1};
            var c = new String('123');
            var d = new Number(2);
            var e = new Array([1,2]);
            console.log(c instanceof String);  //true
            console.log('2' instanceof String); //false
            console.log(d instanceof Number); //true
            console.log(2 instanceof Number); //false
            console.log(e instanceof Array); //true
            console.log([1,2] instanceof Array);//true
            console.log(a instanceof Function); //true
            console.log(b instanceof Object); //true
        </script>

     

  2. instanceof运算符
    1. 用来检测 constructor.prototype 是否存在于目标参数的原型链上;
      obj instanceof Object;//true 实例obj在不在Object构造函数中
      

       

    2. 继承中判断实例是否属于它的父类

      function Person(){};
      function Child(){};
      var p = new Person();
      Child.prototype = p;//继承原型
      var c = new Child();
      console.log(c instanceof Child);//true
      console.log(c instanceof Person);//true
      

       

  3. constructor
    1. constructor 属性返回所有 JavaScript 变量的构造函数
      "John".constructor                 // 返回函数 String()  { [native code] }
      (3.14).constructor                 // 返回函数 Number()  { [native code] }
      false.constructor                  // 返回函数 Boolean() { [native code] }
      [1,2,3,4].constructor              // 返回函数 Array()   { [native code] }
      {name:'John', age:34}.constructor  // 返回函数 Object()  { [native code] }
      new Date().constructor             // 返回函数 Date()    { [native code] }
      function () {}.constructor         // 返回函数 Function(){ [native code] }
      
      
      //注意:null和undefined
      //Uncaught TypeError: Cannot read property 'constructor' of null
         
      //Uncaught TypeError: Cannot read property 'constructor' of undefined
       

       

    2. 使用示例
      1. 检测对象是否为日期
        var myDate = new Date();
        function isDate(date) {
            return date.constructor.toString().indexOf("Date") > -1;  
        }
        
        var isD = isDate(myDate); // true

         

      2. 检测对象是否为数组
        var fruits = ["Banana", "Orange", "Apple", "Mango"];
        
        function isArray(myArray) {
            return myArray.constructor.toString().indexOf("Array") > -1;
        }
        var test = isArray(fruits); //true

         

  • undefined和null

  • null和undefined的定义

    • null:主动释放一个变量引用的对象,表示一个变量不再指向任何对象地址;
    • undefined:是所有没有赋值变量的默认值,自动赋值;
  • null和undefined的区别

    • null和undefined的值相等,但类型不等
    • 都是原始类型,保存在栈中变量本地
    • undefined——表示变量声明过但并未赋过值,它是所有未赋值变量默认值
    • null——表示一个变量将来可能指向一个对象,一般用于主动释放指向对象的引用
typeof undefined             // undefined
typeof null                  // object
null === undefined           // false
null == undefined            // true

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值