ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JS--数据类型与判断

2022-06-18 19:36:42  阅读:156  来源: 互联网

标签:console log -- Object 数据类型 JS typeof constructor true


Js--数据类型:

   分为:原始数据类型(基本数据类型),引用数据类型

原始数据类型:String    Number   Null  undefined   Boolean    Symbol.....

引用数据类型:Object Array   Function ......

基本数据类型和引用数据类型存储在内存中的位置不同:

  • 基本数据类型存储在栈中

  • 引用类型的对象存储于堆中

Js--数据类型的判断

 1  <script>
 2     var arr = [1, 2, 3]
 3     var str = 'abc'
 4     var num = 1
 5     var obj = { a: 0, b: 1 }
 6     var fun = function () { console.log(1); }
 7     var be = true
 8     var nu = null
 9     // 1.typeof
10     // console.log(typeof arr); //object
11     // console.log(typeof str); //string
12     // console.log(typeof num); //number
13     // console.log(typeof obj); //object
14     // console.log(typeof fun); //function
15     // console.log(typeof be); //boolean
16     // console.log(typeof nu); //object
17     // 其中typeof返回的类型都是字符串形式,需注意,例如:
18     // console.log(typeof str == 'string');//true
19     // console.log(typeof num == "number");//true
20     // 判断已知对象类型的方法: instanceof
21     // console.log(arr instanceof Array);//true
22     // console.log(str instanceof String);//false
23     // console.log(obj instanceof Object);//true
24     // console.log(fun instanceof Function);//true
25     // 注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支
26     // 3、根据对象的constructor判断: constructor(构造器/构造函数)
27     // console.log(arr.constructor === Array);//true
28     // console.log(str.constructor == String);//true
29     // console.log(obj.constructor == Object);//true
30     // console.log(num.constructor == Number);//true
31     // console.log(fun.constructor == Function);//true
32     // console.log(be.constructor == Boolean);//true
33     // 注意: constructor 在类继承时会出错
34     // function A() { };
35     // function B() { };
36     // A.prototype = new B(); //A继承自B
37     // var aObj = new A();
38     // console.log(aObj.constructor === B)//true;
39     // console.log(aObj.constructor === A)//false;
40     // 而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
41     // console.log(aObj instanceof B);//true
42     // aObj.constructor = A; //将自己的类赋值给对象的constructor属性
43     // console.log(aObj.constructor === A)// true;
44     // console.log(aObj.constructor === B) //false; //基类不会报true了;
45     //4、通用但很繁琐的方法: prototype
46     console.log(Object.prototype.toString.call(arr) === '[object Array]')//true
47     console.log(Object.prototype.toString.call(str) === '[object String]')//true
48     console.log(Object.prototype.toString.call(num) === '[object Number]')//true
49     console.log(Object.prototype.toString.call(obj) === '[object Object]')//true
50     console.log(Object.prototype.toString.call(fun) === '[object Function]')//true
51     console.log(Object.prototype.toString.call(be) === '[object Boolean]')//true
52     console.log(Object.prototype.toString.call(nu) === '[object Null]')//true
53 
54   </script>

 

标签:console,log,--,Object,数据类型,JS,typeof,constructor,true
来源: https://www.cnblogs.com/jingxin01/p/16389043.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有