1,基本数据类型
js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Undefined,Null) 和一种复杂数据类型(Object)即js按照存储方式分为值类型和引用类型。 数值类型,变量向另一个变量赋值基本类型的值时,仅针对数值对新值拷贝,数据独立使用。 引用类型,变量向另一个变量赋值引用类型的值时,栈区地址指针的赋值,指向对象的引用。 使用var声明了变量,但未给变量初始化值,那么这个变量的值就是undefined,没定于也是undefined 但是受let块级作用域的暂时性死区,未定义的变量进行typeof获取类型会抛出异常报错。ReferenceError instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。 描述:instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上。 语法:object instanceof constructor 例如:Function instanceof Object // Foo instanceof Function //true typeof 一元运算,放在运算数之前,运算数可以是任意类型。返回值类型字符串
2,类型运算
typeof 123 //Number typeof 'abc' //String typeof true //Boolean typeof undefined //Undefined typeof null //Object typeof { } //Object typeof [ ] //Object typeof console.log() //Function typeof 37 === 'number'; typeof 3.14 === 'number'; typeof(42) === 'number'; typeof Math.LN2 === 'number'; typeof Infinity === 'number'; typeof NaN === 'number'; typeof Number(1) === 'number'; typeof "" === 'string'; typeof "bla" === 'string'; typeof (typeof 1) === 'string'; typeof String("abc") === 'string'; typeof true === 'boolean'; typeof false === 'boolean'; typeof Boolean(true) === 'boolean'; typeof undefined === 'undefined'; typeof declaredButUndefinedVariable === 'undefined'; typeof undeclaredVariable === 'undefined'; typeof {a:1} === 'object'; typeof [1, 2, 4] === 'object'; typeof new Date() === 'object'; typeof new Boolean(true) === 'object'; // true Functions typeof new Number(1) === 'object'; // true Functions typeof new String("abc") === 'object';// true Functions typeof null === 'object'; typeof function(){} === 'function'; typeof class C {} === 'function'; typeof Math.sin === 'function'; typeof Symbol() === 'symbol' typeof Symbol('foo') === 'symbol' typeof Symbol.iterator === 'symbol'
3,其他细节
isNaN(123) //false isNaN("hello") //true 字符串有length属性。字符串转换:转型函数String(),适用于任何数据类型 (null,undefined 转换后为null和undefined);toString()方法(null,defined没有toString()方法) 三大引用类型 1.Object类型,第一种是使用new操作符后跟Object构造函数,第二种方式是使用对象字面量表示法 var person = new Object(); person.name = "Micheal"; person.age = 24; var person = { name : "Micheal", age : 24 }; 2.数组的每一项可以用来保存任何类型的数据,数组大小动态调整,Array构造函数,数组字面量表示 var colors = new Array("red","blue","yellow"); var colors = ["red","blue","yellow"]; 3.Function类型,每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法 js == 与 === 的区别 1、对于string,number等基础类型,==(值)和===(值类型) 2、对于Array,Object等高级类型,==和===没有区别(指针地址) 3、基础类型与高级类型作比较,==(高级转化为基础类型,进行“值”比较)和===(类型不同,===结果为false) try { console.log(message); } catch(err) { console.log(err.message); } try { console.log(message); } catch(err) { console.log(err.message); } finally { //forever run here }
4,理解JavaScript中的柯里化和反柯里化
柯里化,可以理解为提前接收部分参数,延迟执行,不立即输出结果,而是返回一个接受剩余参数的函数。因为这样的特性,也被称为部分计算函数。 柯里化,是一个逐步接收参数的过程。在接下来的剖析中,你会深刻体会到这一点。 例如:实现 add(1)(2, 3)(4)() = 10 的效果 反柯里化,是一个泛型化的过程。它使得被反柯里化的函数,可以接收更多参数。目的是创建一个更普适性的函数,可以被不同的对象使用。有鸠占鹊巢的效果。 var fn = function(){}; var val = 1; var toString = Object.prototype.toString.unCurrying(); if(toString(fn) == '[object Function]'){ console.log(`${fn} is function.`); } if(toString(val) == '[object Number]'){ console.log(`${val} is number.`); }