注册
『笔记』JavaScript进阶学习笔记 0
Brains - 灵感乌托邦

『笔记』JavaScript进阶学习笔记 0

六六丶
2021-11-22 / 0 评论 / 5,860 阅读 / 收录检测中...
广告
温馨提示:
本文最后更新于2022年09月26日,已超过577天没有更新,若内容或图片失效,请留言反馈。

数据类型

1、分类

基本(值)类型

对象(引用)类型

2、类型的判断

<script>
        // 1、基本数据类型
        var a;
        // typeof返回数据类型的字符串表达
        console.log(a, typeof a, typeof a==="undefined", a===undefined);//undefined "undefined" true true
        console.log(undefined === 'undefined'); //false
        a = 3;
        console.log(typeof a ==='number');//true
        a = 'abc';
        console.log(typeof a ==='string');//true
        a = true;
        console.log(typeof a === 'boolean');//true
        a= null;
        console.log(typeof a, a===null); //obiect true
        console.log('--------------------')
        // 2、对象类型 
        console.log('对象类型')
        var b1 ={
            b2:[1,'abc',console.log],
            b3:function(){
                console.log('b3');
                return function(){
                    return 'hello';
                };
            }
        };
        // instanceof判断a(实例对象)是不是b(构造函数)类型的实例
        console.log(b1 instanceof Object, b1 instanceof Array); //true  false
        console.log(b1.b2 instanceof Array, b1.b2 instanceof Object);  //true true
        console.log(b1.b3 instanceof Function, b1.b3 instanceof Object); //true true
        console.log(typeof b1.b2,'flag'); //'object'  b1.b2为array(数组)类型
        console.log(typeof b1.b3 === 'function'); //true
        console.log(b1.b2[2] instanceof Function, typeof b1.b2[2] === 'function'); //true true
        b1.b2[2]('你好'); //'你好'
        console.log(b1.b3()()); // hello
        b1.b3(); //可以执行
        /*var obj = {
            name: 'tom',
            age:12
        }; 
        function test() {
            var a = 3;
        };
        var arr = [3, "abc"];
        console.log(arr[1]);*/
        
        // 实例:实例对象
        // 类型:类型对象
        // 函数是一种特殊的对象(可执行)
        function Person(name,age){ //构造函数   类型
            this.name = name;
            this.age = age;
        }
        var p = new Person('tom',12); //根据类型创建的实例对象
        // Person('jack',12);
    </script>

1、undefined与null的区别?

  • undefined 代表定义未赋值
  • null 代表定义并赋值了,但是值为null

2、什么时候给变量赋值为null?

  • 初始赋值:表明将要赋值为对象
  • 结束前:让对象成为垃圾对象(垃圾回收)

3、严格区别变量类型与数据类型?

(1)数据的类型

  • 基本类型
  • 对象类型
    (2)变量的类型(变量内存值的类型)
  • 基本类型:保存的基本类型的数据
  • 引用类型:保存的是地址值
    对象类型即是引用类型
<script>
        var a;
        console.log(a); //undefined,未赋值
        a = null;
        console.log(a); //null,赋值为null

        var b = null; //初始赋值为null,表面将要赋值为对象
        // 确定对象就可以赋值
        b = ['hello',12];
        // 最后,为了释放对象内容
        b = null; //让b指向的对象成为垃圾对象(被垃圾回收器回收:垃圾回收)
        // b = 2;  //又重新赋值了,没有意义

        // c保存的是地址值,即为引用类型
        var c = function(){
        };
        console.log(typeof c) //'function'
</script>

数据、变量、内存

定义

1、什么是数据

  • 存储在内存中代表特点信息的东西,本质上是0101.......
  • 数据的特点:可传递、可运算

2、什么是内存

  • 内存条通电以后产生的可存储数据的空间(临时的)
  • 内存产生和死亡:
    内存条(电路板)==>通电==>产生内存空间==>存储数据==>处理数据==>断电==>内存空间和数据都消失
  • 一块小内存的2个数据

     `内部存储的数据` 
     `地址值`
  • 内存分类

     `栈:保存全局变量/局部变量(变量名)`
     `堆:对象(函数、数组)`  

    3、什么是变量

  • 可变化的量:由变量名、变量值组成
  • 每个变量都对应一块小内存,变量名用来查找对应的内存,变量值就是内存中保存的数据

4、内存、数据、变量三者之间的关系

  • 内存是用来存储数据的空间
  • 变量是内存的标识

kwk263ga.png

<script>
  var age = 18;
  console.log(age)

  var obj = {name:'tom'};
  console.log(obj.name);
  
  function fn(){
    var obj = {name:'tom'};
  };
  
  var a = 3;
  var b = a + 2;
</script>
13
打赏
gzh

评论 (0)

图片
私语
取消
文章目录