简称 JS,运行在浏览器的语言,单线程,弱类型语言。

变量声明

var、let、const 的区别

var

  • 函数作用域

    1. var bar = 'bar1'
    2. function print(){
    3. var bar = 'bar2'
    4. console.log(bar)// ?
    5. }
    6. print()
  • 声明提升

    1. console.log(bar)// ?
    2. var bar = 'bar'
    3. console.log(bar)// ?
  • 浏览器内全局声明会挂载到 window

    1. var bar = 'bar';
    2. console.log(bar)// ?

    let

  • 块级作用域{}

    1. for(var i = 0; i < 5; i++){
    2. console.log(i); // ?
    3. }
    4. consoloe.log(i); // ?
  • 声明不提升

    1. console.log(bar);
    2. let bar = 'bar'

    const

  • 块级作用域{}

  • 赋值之后不可变
    1. const PI = Math.PI;
    2. PI = 3;// Error

非必要不使用 var,变量使用 let,常量使用 const

对象(object)

主要的数据类型,主要了解 object 的引用类型的特点

  1. let person0 = {name: '小明',age: 23};
  2. let person1 = person0;
  3. person0.age = 20;
  4. console.log(person1.age)// ?

浅拷贝

  • Object.assign({},person)

    深拷贝

  • JSON.stringify(person)

  • 借助 lodash 的 cloneDeep 函数

    原型

    this 的指向

    四种 this 的指向

  • 全局下指向window

  • 谁调用就指向谁
  • 构造函数指向实例
  • apply、call、bind指向第一个参数

    函数(function)**

    函数的定义

  • 函数声明(声明提升)

    1. print()
    2. function print(){console.log('Hello World)}
  • 函数表达式

    1. let add = function (){...}
    2. // or
    3. let obj={
    4. add: function(){}
    5. }
    6. // es6 语法
    7. let obj={
    8. add(){...}
    9. }
  • 立即执行函数(this 指向 window)

    1. (function (){...})()

    箭头函数

  • 箭头函数的写法

    1. ()=>{}
  • 箭头函数的 this 指向

    闭包

    异步方法

    Ajax、settimeout

    ```javascript goStudy(e) { ewx.request({

    1. url: `/api/Study/StudyCheck`,
    2. method: 'POST',
    3. data: {
    4. projectId: data.id
    5. },
    6. success(res) {
    7. wx.showModal({
    8. title: '温馨提示',
    9. content: '头像用于人脸识别的对比照片,请先完善个人资料',
    10. success(res) {
    11. wx.chooseImage({
    12. sizeType: ['compressed'],
    13. sourceType: ['album', 'camera'],
    14. count: 1,
    15. success(res) {
    16. // console.log(res)
    17. const tempFilePaths = res.tempFilePaths
    18. wx.showLoading({
    19. title: "上传中…",
    20. })
    21. console.log(tempFilePaths[0])
    22. ewx.uploadFile({
    23. url: `/api/Student/UpdatePhoto`,
    24. filePath: tempFilePaths[0],
    25. name: 'file',
    26. formData: {
    27. 'user': 'test'
    28. },
    29. success(res) {
    30. wx.showToast({
    31. title: "上传成功",
    32. icon: "none",
    33. duration: 1000
    34. })
    35. },
    36. fail(err){
    37. // ..
    38. }
    39. })
    40. },
    41. fail(err){
    42. // ..
    43. }
    44. })
    45. },
    46. fail(err){
    47. // ...
    48. }
    49. })
    50. }

    }) }

  1. <a name="r9tdG"></a>
  2. ### 解决回调地狱(callback hell)
  3. - promise
  4. ```javascript
  5. function prRequest() {
  6. new Promise((resolve, reject) => {
  7. wx.request({
  8. url: 'xx',
  9. success: (res)=>{
  10. resolve(res)
  11. },
  12. fail: (err)=>{
  13. reject(err)
  14. }
  15. })
  16. })
  17. }
  18. prRequest.then(res=>{console.log(res)})
  • async、await
    1. (async ()=>{
    2. let id = await this.ajax1()
    3. let res = await this.ajax2(id)
    4. console.log(res)
    5. })()

模块(module)

  • import
  • export