咏竹莉
where there is a will,there is a way

闭包

A函数中有个B函数,B函数可以访问A函数内部的变量,B函数就形成闭包

闭包导致垃圾回收机制没有将当前变量回收掉,这样的操作,可能会造成内存泄露,好处就是可以设计私有的方法和变量。

 

垃圾回收机制(闭包的延伸)

js拥有特殊的垃圾回收机制,当一个变量在内存中失去引用,js会通过特殊的算法将其回收,释放内存。

 

捕获和冒泡

捕获: 就是从根元素像目标元素递进的一个关系,从上而下

冒泡: 从目标元素开始向根元素冒泡的过程; 想象一下水里的泡泡,从下而上

stopPropagation 通常理解它是来阻止事件冒泡的,其实该函数可以阻止捕获事件

 

typeof

typeof检测数据类型, 函数是function类型之外,常见的数组,对象或者正则,都是object类型

typeof Symbol()  // 'symbol'
typeof null  // object
typeof  undefined // object

 

js中常见的数据类型

number、 boolean、string、undefined、null、Object( function、array、Date )

 

隐式转换

1. + 和 -

巧用+ 和 - 规则转换类型

把变量转换为数字: num-0;

把变量转换为字符串: num+""

2. == 和=== 的区别

== 用于一般比较,==在比较的时候可以转换数据类型,转化为同一类型后,看值是否相等

===用于严格比较,只要类型不匹配,就返回false

 

数据类型检测

javascript中类型检测方法有很多:

· typeof

· instanceof

· Object.prototype.toString

· constructor

 

1. typeof

console.log(typeof 100) // "number"
console.log(typeof true)  // "boolean"
console.log(typeof [1,2])    // "Object"
console.log(typeof undefined)  // "undefined"
console.log(typeof new Object())  // "object"
console.log(typeof NaN)   // "number"
console.log(typeof null) // "object"
console.log(typeof '123')  // "string"

比较特殊的是 typeof null 返回 "object"

typeof 可以识别出基本类型 number、string、boolean、undefined、symbol,但不能识别null,。不能识别引用数据类型,会把null、array、object统一归为object类型。

 

2.  instanceof

检测机制js原型继承机制

let bool = true;
let num = 1;
let str = 'abc';
let  und= undefined;
let nul = null;
let arr = [1,2,3,4];
let obj = {name:'xiaoming',age:22};
let fun = function(){console.log('hello')};
let s1 = Symbol();

 

console.log(bool instanceof Boolean);// false
console.log(num instanceof Number);// false
console.log(str instanceof String);// false
console.log(und instanceof Object);// false
console.log(nul instanceof Object);// false
console.log(arr instanceof Array);// true
console.log(obj instanceof Object);// true
console.log(fun instanceof Function);// true
console.log(s1 instanceof Symbol);// false

instanceof 不能识别出基本的数据类型 number、boolean、string、undefined、null、symbol

可以检测出引用类型,如array、object、function, instanceof一般用来检测对象类型,以及继承关系。

3. constructor

console.log(bool.constructor === Boolean);  // true
console.log(num.constructor  == Number);   // true
console.log(arr.constructor === Array);  // true
console.log(obj.constructor === Object);  // true  

null、undefined 没有constructor方法, 因此constructor不能判断undefined 和null。

 

原型、原型链

原型链: 每个被实例对象都有_proto_对象,它指向了构造该函数的构造函数的prototype属性。同时该对象可以通过_proto_对象来寻找不属于自身的属性。

原型: 实现继承过程中产生的一个概念

 

简述深浅拷贝

浅拷贝

通常需要拷贝的对象内部只有一层的对象

常用的方法

1. Object.assign  方法来实现

2. 拓展运算符  ...obj

 

深拷贝

通常是嵌套二层或以上的复杂对象

常用方法

1. JSON.parse(JSON.stringfy(object));   该方法忽略到undefined、忽略掉Symbl、忽略function。只适合简单拷贝

2. 手写递归方法去实现

3. 三方库

 

JSON.parse() 与JSON.stringfy() 的区别

JSON.parse(str)   【从一个字符串中解析出json对象(键值对)】

// 定义一个字符串
var
str = '{"name": "小猪"}' // 注意单引号写在{}外,每个属性名必须用双引号,否则会抛出异常
// 解析对象
JSON.parse(str); // 结果 name: "小猪"

 

JSON.stringify()  【从一个对象中解析出字符串】

 

var data = {name:"xiaozhu"}
// 解析对象
JSON.stringify(data)
// 结果 '{"name":"xiaozhu"}'

 

posted on 2021-03-10 15:36  咏竹莉  阅读(60)  评论(0)    收藏  举报