(1) JS data type

This article will sort out the issues related to JS data types and show each knowledge point with code. Content will continue to be added in the future~


(1) JS data type

  1. Basic data types—save simple values

undefined, null, boolean, string, number, symbol (ES6, mainly to prevent naming conflicts)

  1. Reference data type—holds objects composed of multiple values

Collectively referred to as the Object type.
Subdivided into Object type, Function type, Array type, Date type, RegExp type

Main difference: different storage locations

Basic data types is stored directly in the stack ; the reference data stored in the object entity type stack , the stack pointer is stored, pointing to the start position of the stack entity.

Insert picture description here

(2) Basic packaging type

1. What is it? —Number, String, Boolean in the basic data types are the basic packaging types.
2. Why was it introduced? —In order to facilitate the use of these simple data, such as obtaining the length of a string, intercepting a string, etc.
3. How to use it? -Literal creation, new creation
In fact, the basic packaging type should belong to a constructor , for example, when creating a string, the background will create an instance of the string type for us. We can use its properties and methods.

 <script>
        //字面量创建
        var arr1 = "arr1";
        //new创建字符串
        var arr2 = new String("arr2");

        //输出
        console.log(arr1);
        console.log(arr2);

        //1、两种创建方式都可以使用对应的属性与方法
        console.log(arr1.length);
        console.log(arr2.length);
        console.log(arr1.slice(1));
        console.log(arr2.slice(2,3));

        //2.用new创建的可以添加新的属性和方法
        // arr1.age=4;
        // arr1.name=function(){return "zhangsan"};
        // console.log(arr1.age);
        // console.log(arr1.name());
        arr2.age=4;
        arr2.name=function(){
    
    return "lisi"};
        console.log(arr2.age);
        console.log(arr2.name());
    </script>

Insert picture description here
arr1 cannot add attributes and methods
Insert picture description here

(3) Judge the type of variable

There are four common ones:
typeof variable
A instanceof B
object.constructor===data type
Object.prototype.call()

Code Description

  <script>
        /* 1)typeof判断除null以外的基本数据类型均返回正确的结果,
              null返回object类型。
             对于引用数据类型,除function类型返回function,
             其它均返回object类型
        */
       console.log(typeof('')); 
       console.log(typeof(null));
       console.log(typeof(new Date()));
       console.log(typeof({
    
    }));
       var a = function(){
    
    return 10;}
       console.log(typeof(a));
    </script>

Insert picture description here
②(Dig hole) Jump ☞ prototype chain


       /* 2) A instanceof B: A是实例,B是构造函数。
             原理:检查B的原型对象prototype是否在A的原型链上
       */
      var a = new Date();
      console.log(a instanceof Date);

Only return true or false ③Remember
Insert picture description here
: every object instance can access its constructor through the constrcutor object

/* 3) 每一个对象实例都可以通过 constrcutor 对象访问它的构造函数*/
        var arr = new Array();
        console.log(arr.constructor)
        console.log(arr.constructor === Array)

Insert picture description here
④ Digging: Jump ☞ scope, scope chain and execution context

/*4)借用tostring方法直接输出变量的数据类型 */
        var arr3 = new Array();
        console.log(Object.prototype.toString.call(arr3));
        console.log(Object.prototype.toString.call(1));
        console.log(Object.prototype.toString.call(undefined));
        console.log(Object.prototype.toString.call(new Date()));

Insert picture description here

(4) Conversion of data types

<script>
        /*
        1)String-->Number
          显式(强制)转换:Number()、parseInt()、parseFloat()
          隐式转换:减乘除取余???(只有-不报错)
        2)其它-->String
          显式:tostring()、String()
          隐式:+""
        3)其它-->Boolean
          显式:Boolean()
          隐式:前面加!!
        */

        //1)String-->Number
        var a = '123' 
        var b = '123'
        b=-b
        console.log("a变成了"+typeof(parseInt(a)));
        console.log("b变成了"+typeof(parseInt(b)));
        //2)其它-->String
        var c = 123
        var d = 123
        console.log("c变成了"+typeof((c.toString())));
        console.log("d变成了"+typeof((d+"")));
        //3)其它-->Boolean
        var e ='123'
        console.log("e变成了"+typeof(!!e));
    </script>

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42232573/article/details/113061952
Recommended