由于近期学习的东西比较多,JS这一块的要点尤其多,而且杂,出于方便自己记忆和理解的原因,所以写下这篇总结,包括JS面试一些出现频率较高的知识点,我会在后期的学习不断更新这篇博客。

JS7种数据类型

简单类型

null、undefined、string、number、boolean、symbol

复杂类型

object

详见我的博客:JS数据类型

JS中的类型转换

博客:JS数据类型-二

博客中提到三种类型之间的互相转换,忘记的时候可以翻阅。

JS函数

JS数组

闭包

闭包总是和立即执行函数一起出现,立即执行函数是为了解决在命名变量时不使用全局变量,而闭包的精髓就是这个函数引用到了函数之外的变量。

1
2
3
4
5
6
!function(){
var a = 0 // 1
window.add = function(){ // 2
return a++
}
}()

上述代码中 1 和 2 组成了一个闭包 这时用户可以使用 add()函数操作a,却不能直接访问到a变量,因为a在一个立即执行函数中。

原型、继承、new

new的作用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function Human(){  // 原型
walk = function(){
console.log('walk')
}
talk = function(){
console.log('talk')
}
}

var human = new Human({name:'lee',city:'wuhan'})
// 这个过程
// function Human(){
// var temp = {} 1
// this = temp 2
// this.__proto__ = Human.prototype 3
// this.name = 'lee'
// this.city = 'wuhan'
// return temp 4
// }

上述代码中 1 2 3 4 就是new的过程
声明一个临时对象,将this指向临时对象,将临时对象的__proto__(原型链) 指向 原型的prototype(共有属性),返回这个临时对象
而其实中this.namethis.city就是这个new出来的对象的私有属性。

AJAX

AJAX实际上只有4行代码。

1
2
3
4
5
6
7
8
9
10
11
12
var xhr = xmlHttpRequest()
xhr.open('/xxx','post',true) // 3个参数 url method boolean(是否异步)默认为true
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){ // 请求是否加载完毕
if(xhr.status >= 200 && xhr.status < 300){ // 判断响应状态是否成功
successfn.call(undefined,xhr.responseText) // 成功就调用successFn(这是一个回调函数)
}else if(xhr.status >= 400){ // 失败状态
failFn.call(undefined,xhr.status) // 调用failFn(同样是一个回调函数)
}
}
}
xhr.send()

异步

__END__

o0Chivas0o
文章作者:o0Chivas0o
文章出处JS要点整理
作者签名:Rich ? DoSomethingLike() : DoSomethingNeed()
版权声明:文章除特别声明外,均采用 BY-NC-SA 许可协议,转载请注明出处