js基础复习
// 数据类型
JavaScript的类型有 数值number 字符串string 布尔值boolean 函数function 对象object null undefined 数组Array 日期Data 正则表达式RegExp; symbol我没用过不是很清楚
// 数值类型转布尔值结果 用 == 比较 如 1 == true ,隐式转换
undefined ==> false ,不能全等于false ,可以全等于undefined
null ==> false ,不能全等于false ,可以全等于null
boolean ==> true是true,false是false ,可以全等于true或者false
number ==> 0,+0,-0和NaN都是false,其他的全是true ,不能全等于布尔值,可以全等于数值
string ==> 长度为0的字符串是false,其他都是true,字符串里是空格也是true ,不能全等于布尔值,可以全等相同字符串
Array ==> false,[] == [] 为false, [] === [] 为false, 但是Boolean([]) 为true,所以if判断条件是用空数组的话,是为true的
object ==> true,空对象,function,日期Data,正则表达式RegExp,也是true
//经过 typeof 运算符 后展示的类型,打印显示都是小写字母
let num = 0; // number 数值
let str = "haha"; // string 字符串
let flag = true; // boolean 布尔值
let unde; // undefined
let fn = function(){console.log('haha');}; // function 函数
let nullVal = null; // object null
let obj = {name:'yu'}; // object 对象
let reg = /[0|7]+/; // object 正则
let time = new Date(); // object 时间
let arr = [1,2]; // object 数组
// 经过 instanceof 运算符 后会展示布尔类型,如 str instanceof String
// instanceof 后对应数据类型,只能检测引用数据类型 类型大驼峰
let str = '1'; // str instanceof String ==> false 因为不能检测基本数据类型
let num = 1; // num instanceof Number ==> false 因为不能检测基本数据类型
let obj = {}; // obj instanceof Object ==> true
let fn = function(){}; // fn instanceof Object ==> true; fn instanceof Function ==> true;
let arr = []; // arr instanceof Object ==> true; arr instanceof Array ==> true;
let reg = /12/; // reg instanceof Object ==> true; reg instanceof RegExp ==> true;
let time = new Date();; // time instanceof Object ==> true; time instanceof Date ==> true;
// Object.prototype.toString() 可以用来准确地检测所有数据类型
Object.prototype.toString.call(1) //"[object Number]"
Object.prototype.toString.call('111') //"[object String]"
Object.prototype.toString.call(true) //"[object Boolean]"
Object.prototype.toString.call(null) //"[object Null]"
Object.prototype.toString.call(undefined) //"[object Undefined]"
Object.prototype.toString.call([]) //"[object Array]"
Object.prototype.toString.call({}) //"[object Object]"
Object.prototype.toString.call(function fn() {}) //"[object Function]"
// this指向理解
// 1, this指向与一般的function函数不同
声明式 function fun() { } //this指向 ==> window
赋值式 var fun = function () { } //this指向 ==> window
forEach()循环 //this指向 ==> window
定时器, 延时器 setInterval(function () { }, 时间) //this指向 ==> window
对象中的函数 const obj = { fun: function () { } } // this指向 ==> obj对象
事件处理函数 标签.addEventListener(事件类型, function () { }) // this指向 ==> 标签对象
箭头函数的this指向, 是父级程序的this指向
如果父级程序有this指向, 指向向的就是父级程序的this指向
如果父级程序没有this指向(对象, 数组是没有this), 指向的是window
// 2, 箭头函数, 无法改变this指向
// 3, 改变this指向
函数.call(参数1, 其他参数...)
立即执行函数, 并且改变函数的this指向为参数1表示的内容
其他参数, 是原始函数的实参, 多个实参使用逗号间隔
函数.apply(参数1, [数组参数])
立即执行函数, 并且改变函数的this指向为参数1表示的内容
数组参数, 是原始函数的参数, 多个参数以数组的单元的形式存储
函数.bind(参数1)
不会立即执行函数, 而是生成一个新的函数
新函数, 参数, 程序内容, 都与原始函数相同
只是this改变为参数1表示的内容
// 所谓的箭头函数,是函数的另一种语法形式
const fun = function () { } 普通函数
const fun = () => { } 箭头函数
将匿名函数的部分, 从 function() { } 写成()=> { } 的形式
如果函数只有一个参数, 可以不写()
const fun = function (e) { } 普通函数
const fun = e => { } 箭头函数
如果执行体中只有一行代码, 可以不写{ }
const fun = e => { console.log(e) } 普通箭头函数
const fun = e => console.log(e) 不写{ }箭头函数
// 检测执行效率
let FunTime = function () {
let arr = [0,1,2,3]
arr.map(item=>{
console.log(item);
})
}
// 可以用下面的方法检测执行效率,b格拉满
function t(){
console.time("a");
FunTime();
console.timeEnd("a");
}
t();