简称 JS,运行在浏览器的语言,单线程,弱类型语言。
变量声明
var
函数作用域
var bar = 'bar1'function print(){var bar = 'bar2'console.log(bar)// ?}print()
声明提升
console.log(bar)// ?var bar = 'bar'console.log(bar)// ?
浏览器内全局声明会挂载到
windowvar bar = 'bar';console.log(bar)// ?
let
块级作用域
{}for(var i = 0; i < 5; i++){console.log(i); // ?}consoloe.log(i); // ?
声明不提升
console.log(bar);let bar = 'bar'
const
块级作用域
{}- 赋值之后不可变
const PI = Math.PI;PI = 3;// Error
非必要不使用 var,变量使用 let,常量使用 const
对象(object)
主要的数据类型,主要了解 object 的引用类型的特点
let person0 = {name: '小明',age: 23};let person1 = person0;person0.age = 20;console.log(person1.age)// ?
浅拷贝
-
深拷贝
JSON.stringify(person)
借助 lodash 的 cloneDeep 函数
原型
this 的指向
四种
this的指向全局下指向
window- 谁调用就指向谁
- 构造函数指向实例
-
函数(function)**
函数的定义
函数声明(声明提升)
print()function print(){console.log('Hello World)}
函数表达式
let add = function (){...}// orlet obj={add: function(){}}// es6 语法let obj={add(){...}}
立即执行函数(this 指向 window)
(function (){...})()
箭头函数
箭头函数的写法
()=>{}
-
闭包
异步方法
Ajax、settimeout
```javascript goStudy(e) { ewx.request({
url: `/api/Study/StudyCheck`,method: 'POST',data: {projectId: data.id},success(res) {wx.showModal({title: '温馨提示',content: '头像用于人脸识别的对比照片,请先完善个人资料',success(res) {wx.chooseImage({sizeType: ['compressed'],sourceType: ['album', 'camera'],count: 1,success(res) {// console.log(res)const tempFilePaths = res.tempFilePathswx.showLoading({title: "上传中…",})console.log(tempFilePaths[0])ewx.uploadFile({url: `/api/Student/UpdatePhoto`,filePath: tempFilePaths[0],name: 'file',formData: {'user': 'test'},success(res) {wx.showToast({title: "上传成功",icon: "none",duration: 1000})},fail(err){// ..}})},fail(err){// ..}})},fail(err){// ...}})}
}) }
<a name="r9tdG"></a>### 解决回调地狱(callback hell)- promise```javascriptfunction prRequest() {new Promise((resolve, reject) => {wx.request({url: 'xx',success: (res)=>{resolve(res)},fail: (err)=>{reject(err)}})})}prRequest.then(res=>{console.log(res)})
- async、await
(async ()=>{let id = await this.ajax1()let res = await this.ajax2(id)console.log(res)})()
模块(module)
- import
- export
