第一章 JavaScript的数据类型

87 阅读4分钟

数据类型:

1.基本数据类型

   Number , String , Boolean , Null , Undefined , Symbol , BigInt8String(字符串类型):  "字符串"
   Number(数组类型):  10
   Boolean(布尔类型):  true || false  // 只有这两种
   Null(对空):  null
   Undefined(未定义): undefined
   // ES6新增类型
   Symbolsymbol): Symbol('key') //表示独一无二的值,主要是用来定义对象的唯一属性名
//symbol 示例子
let sy = Symbol("KK"); 
console.log(sy); // Symbol(KK) 
typeof(sy); // "symbol"
//基本用法 (写法一)
let obj = {
name:'测试'
}
obj[Symbol('name')] = '测试1'
console.log(obj); // {name:'测试',Symbol(name): "测试1"}
//写法二
let obj = { 
    name:'测试',
    [Symbol('name')]:'测试1'
}
console.log(obj); // {name:'测试',Symbol(name): "测试1"}
//写法三
let obj = {}
Objiect.defineProperty(obj,Symbol('name'),{value:'测试1'})
console.log(obj); // {name:'测试',Symbol(name): "测试1"}

1.JS 提供 Number.MAX_SAFE_INTEGER 常量来表示 最大安全整数, Number.MIN_SAFE_INTEGER 常量表示最小安全整数

1.JS 中的 Number 类型只能安全地表示 -9007199254740991 (-(2^53-1))'9007199254740991(2^53-1) 之间的整数,任何超出此范围的整数值都可能失去精度,所以当运算超过这个范围就需要去使用 BigInt BigInt(大整型): BigInt(2) // 2n 注意: BigInt 不能直接跟 Number 类型进行运算 , BigInt文字 也可以使用二进制、八进制或十六进制表示(不能使用传统的八进制

// binary
console.log(0b100000000000000000000000000000000000000000000000000011n);
// → 9007199254740995n

// hex
console.log(0x20000000000003n);
// → 9007199254740995n

// octal
console.log(0o400000000000000003n);
// → 9007199254740995n

// note that legacy octal syntax is not supported
console.log(0400000000000000003n);
// → SyntaxError

记住: 不能使用严格相等运算符将BigInt与常规数字进行比较,因为它们的类型不同

console.log(10n === 10);    // → false

console.log(typeof 10n);    // → bigint
console.log(typeof 10);     // → number

除一元加号(+)运算符外,所有算术运算符都可用于BigInt

10n + 20n;    // → 30n
10n - 20n;    // → -10n
+10n;         // → TypeError: Cannot convert a BigInt value to a number
-10n;         // → -10n
10n * 20n;    // → 200n
20n / 10n;    // → 2n
23n % 10n;    // → 3n
10n ** 3n;    // → 1000n  幂运算简写形式

const x = 10n;
++x;          // → 11n
--x;          // → 9n

除法(/)运算符的结果会自动向下舍入到最接近的整数

25 / 10;      // → 2.5
25n / 10n;    // → 2n
// 关系运算符不影响
10n > 5;    // → true

注意: 转换BigInt是一个极其复杂的过程,这会导致严重的运行时性能损失。目前,更好的选择是使用JSBI库,它是BigInt提案的纯JS实现

2.引用数据类型:Object

  里面包含的 objectArrayFunctionDateRegExp(正则)、Math(数学对象)。
  
  object(对象): let obj = {}     // let obj = new Object()
  Array(数组): let arr = [1,2,3] // let arr = new Array(1,2,3)
  Function(函数): function fn(){}  // 一种特殊的对象(可以被执行)
  Date(时间对象): let date = new Date() //一种特别的对象(数值下标,内部数据是有序的)

3.类型判断

1.第一种判断方法 typeof

  //typeof返回的类型都是字符串形式
  typeof:可以判断:undefined、数值、字符串、布尔值  不能判断: nullobject object与array
示例
let str = "iamstring.";
let num = 222;
let arr= [1,2,3];
let time = new Date();
let fn = function(){alert(111);};
let n = null;
let u = undefined;

alert(typeof str)  ------------> string
alert(typeof num)  ------------> number
alert(typeof arr)  ------------> object
alert(typeof time)  ------------> object
alert(typeof fn)  ------------> function

2.第二种判断方法 instanceof

  instanceof:可以判断对象的具体类型
示例
   alert(arr instanceof Array) ---------------> true
   alert(fn instanceof Function) ------------> true
   alert(fn instanceof function) ------------> false //注意大小写

注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

3.第三种判断方法 ===

  ===: 可以判断undefinednull
示例
alert(n === null) ---------------> true
alert(u === undefined) ---------------> true

4.第四种判断方法 constructor

alert(arr.constructor === Array) ----------> true
alert(time.constructor === Date) -----------> true
alert(fn.constructor === Function) -------> true

注意: constructor 在类继承时会出错

示例
function A(){};
function B(){};
A.prototype = new B(); //A继承自B
var obj = new A();
alert(obj.constructor === B) -----------> true;
alert(obj.constructor === A) -----------> false;

//解决construtor的问题通常是让对象的constructor手动指向自己
obj.constructor = A; //将自己的类赋值给对象的constructor属性
alert(obj.constructor === A) -----------> true;
alert(obj.constructor === B) -----------> false; //基类不会报true了;

5.第五种判断方法 prototype ( 比较繁琐 )

alert(Object.prototype.toString.call(str) === '[object String]') -------> true;
alert(Object.prototype.toString.call(num) === '[object Number]') -------> true;
alert(Object.prototype.toString.call(arr) === '[object Array]') -------> true;
alert(Object.prototype.toString.call(time) === '[object Date]') -------> true;
alert(Object.prototype.toString.call(fn) === '[object Function]') -------> true;

注意:大小写不能写错,比较麻烦,但胜在通用,基本可以判断所有类型。

6.第六种判断方法 jquery.type()

// 如果对象是undefined或null,则返回相应的“undefined”或“null”.

jQuery.type( undefined ) === "undefined"
jQuery.type() === "undefined"
jQuery.type( window.notDefined ) === "undefined"
jQuery.type( null ) === "null"

// 如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,我们返回相应的 [[Class]] 名字.
jQuery.type( true ) === "boolean"
jQuery.type( 3 ) === "number"
jQuery.type( "test" ) === "string"
jQuery.type( function(){} ) === "function"
jQuery.type( [] ) === "array"
jQuery.type( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"
jQuery.type( true ) === "boolean"
jQuery.type( 3 ) === "number"
jQuery.type( "test" ) === "string"
jQuery.type( function(){} ) === "function"
jQuery.type( [] ) === "array"
jQuery.type( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"

//其他一切都将返回它的类型“object”。