zoukankan      html  css  js  c++  java
  • Document

    常用:

    let关键字:

    1. 作用:
    * 与var类似, 用于声明一个变量
    2. 特点:
    * 在块作用域内有效
    * 不能重复声明
    * 不会预处理, 不存在提升
    3. 应用:
    * 循环遍历加监听
    * 使用let取代var是趋势

     1 //console.log(age);// age is not defined
     2     let age = 12;
     3     //let age = 13;不能重复声明
     4     console.log(age);
     5     let btns = document.getElementsByTagName('button');
     6     for(let i = 0;i<btns.length;i++){
     7             btns[i].onclick = function () {
     8                 alert(i);
     9             }
    10     }
    View Code

    const关键字:

    1. 作用:
    * 定义一个常量
    2. 特点:
    * 不能修改
    * 其它特点同let
    3. 应用:
    * 保存不用改变的数据

    1   const sex = '男';
    2     console.log(sex);
    3     //sex = '女';//不能修改
    4     console.log(sex);
    View Code

    变量的解构赋值:

    1. 理解:
    * 从对象或数组中提取数据, 并赋值给变量(多个)
    2. 对象的解构赋值
    let {n, a} = {n:'tom', a:12}
    3. 数组的解构赋值
    let [a,b] = [1, 'atguigu'];
    4. 用途
    * 给多个形参赋值

     1     let obj = {name : 'kobe', age : 39};
     2 //    let name = obj.name;
     3 //    let age = obj.age;
     4 //    console.log(name, age);
     5     //对象的解构赋值
     6     let {age} = obj;
     7     console.log(age);
     8 //    let {name, age} = {name : 'kobe', age : 39};
     9 //    console.log(name, age);
    10 
    11     //3. 数组的解构赋值  不经常用
    12     let arr = ['abc', 23, true];
    13     let [a, b, c, d] = arr;
    14     console.log(a, b, c, d);
    15     //console.log(e);
    16     function person(p) {//不用解构赋值
    17         console.log(p.name, p.age);
    18     }
    19     person(obj);
    20 
    21     function person1({name, age}) {
    22      console.log(name, age);
    23     }
    24     person1(obj);
    View Code

    模板字符串:

    1. 模板字符串 : 简化字符串的拼接
    * 模板字符串必须用 `` 包含
    * 变化的部分使用${xxx}定义

    1     let obj = {
    2         name : 'anverson',
    3         age : 41
    4     };
    5     console.log('我叫:' + obj.name + ', 我的年龄是:' + obj.age);
    6 
    7     console.log(`我叫:${obj.name}, 我的年龄是:${obj.age}`);
    View Code

    简化的对象写法:

    简化的对象写法
    * 省略同名的属性值
    * 省略方法的function
    * 例如:
    let x = 1;
    let y = 2;
    let point = {
    x,
    y,
    setX (x) {this.x = x}
    };

     1    let x = 3;
     2     let y = 5;
     3     //普通额写法
     4 //    let obj = {
     5 //        x : x,
     6 //        y : y,
     7 //        getPoint : function () {
     8 //            return this.x + this.y
     9 //        }
    10 //    };
    11     //简化的写法
    12     let obj = {
    13         x, //同名的属性可以省略不写
    14         y,
    15         getPoint(){
    16             return this.x
    17         }
    18     };
    19     console.log(obj, obj.getPoint());
    View Code

    箭头函数:

    * 作用: 定义匿名函数
    * 基本语法:
    * 没有参数: () => console.log('xxxx')
    * 一个参数: i => i+2
    * 大于一个参数: (i,j) => i+j
    * 函数体不用大括号: 默认返回结果
    * 函数体如果有多个语句, 需要用{}包围,若有需要返回的内容,需要手动返回
    * 使用场景: 多用来定义回调函数

    * 箭头函数的特点:
    1、简洁
    2、箭头函数没有自己的this,箭头函数的this不是调用的时候决定的,而是在定义的时候处在的对象就是它的this
    3、扩展理解: 箭头函数的this看外层的是否有函数,
    如果有,外层函数的this就是内部箭头函数的this,
    如果没有,则this是window。

     1 let fun = function () {
     2        console.log('fun()');
     3    };
     4    fun();
     5    //没有形参,并且函数体只有一条语句
     6    let fun1 = () => console.log('fun1()');
     7     fun1();
     8    console.log(fun1());
     9     //一个形参,并且函数体只有一条语句
    10     let fun2 = x => x;
    11     console.log(fun2(5));
    12     //形参是一个以上
    13     let fun3 = (x, y) => x + y;
    14     console.log(fun3(25, 39));//64
    15 
    16     //函数体有多条语句
    17     let fun4 = (x, y) => {
    18         console.log(x, y);
    19         return x + y;
    20     };
    21     console.log(fun4(34, 48));//82
    22 
    23     setTimeout(() => {
    24         console.log(this);
    25     },1000)
    26 
    27    let btn = document.getElementById('btn');
    28    //没有箭头函数
    29    btn.onclick = function () {
    30        console.log(this);//btn
    31    };
    32    //箭头函数
    33    let btn2 = document.getElementById('btn2');
    34 
    35     let obj = {
    36         name : 'kobe',
    37         age : 39,
    38         getName : () => {
    39             btn2.onclick = () => {
    40                 console.log(this);//obj
    41             };
    42         }
    43     };
    44     obj.getName();
    45 
    46 
    47  function Person() {
    48      this.obj = {
    49          showThis : () => {
    50              console.log(this);
    51          }
    52      }
    53  }
    54     let fun5 = new Person();
    55     fun5.obj.showThis();
    View Code

    3点运算符:

    1. rest(可变)参数
    * 用来取代arguments 但比arguments灵活,只能是最后部分形参参数
    function add(...values) {
    let sum = 0;
    for(value of values) {
    sum += value;
    }
    return sum;
    }
    2. 扩展运算符
    let arr1 = [1,3,5];
    let arr2 = [2,...arr1,6];
    arr2.push(...arr1);

     1    function fun(...values) {
     2         console.log(arguments);
     3 //        arguments.forEach(function (item, index) {
     4 //            console.log(item, index);
     5 //        });
     6         console.log(values);
     7         values.forEach(function (item, index) {
     8             console.log(item, index);
     9         })
    10     }
    11     fun(1,2,3);
    12 
    13     let arr = [2,3,4,5,6];
    14     let arr1 = ['abc',...arr, 'fg'];
    15     console.log(arr1);
    View Code

    形参默认值:

    * 形参的默认值----当不传入参数的时候默认使用形参里的默认值
    function Point(x = 1,y = 2) {
    this.x = x;
    this.y = y;
    }

     1   //定义一个点的坐标
     2     function Point(x=12, y=12) {
     3         this.x = x;
     4         this.y = y;
     5     }
     6     let point = new Point(25, 36);
     7     console.log(point);
     8     let p = new Point();
     9     console.log(p);
    10     let point1 = new Point(12, 35);
    11     console.log(point1);
    View Code

    Promise对象:

    1. 理解:
    * Promise对象: 代表了未来某个将要发生的事件(通常是一个异步操作)
    * 有了promise对象, 可以将异步操作以同步的流程表达出来, 避免了层层嵌套的回调函数(俗称'回调地狱')
    * ES6的Promise是一个构造函数, 用来生成promise实例
    2. 使用promise基本步骤(2步):
    * 创建promise对象
    let promise = new Promise((resolve, reject) => {
    //初始化promise状态为 pending
    //执行异步操作
    if(异步操作成功) {
    resolve(value);//修改promise的状态为fullfilled
    } else {
    reject(errMsg);//修改promise的状态为rejected
    }
    })
    * 调用promise的then()
    promise.then(function(
    result => console.log(result),
    errorMsg => alert(errorMsg)
    ))
    3. promise对象的3个状态
    * pending: 初始化状态
    * fullfilled: 成功状态
    * rejected: 失败状态
    4. 应用:
    * 使用promise实现超时处理

    * 使用promise封装处理ajax请求
    let request = new XMLHttpRequest();
    request.onreadystatechange = function () {
    }
    request.responseType = 'json';
    request.open("GET", url);
    request.send();

     1 //创建一个promise实例对象
     2     let promise = new Promise((resolve, reject) => {
     3         //初始化promise的状态为pending---->初始化状态
     4         console.log('1111');//同步执行
     5         //启动异步任务
     6         setTimeout(function () {
     7             console.log('3333');
     8             //resolve('atguigu.com');//修改promise的状态pending---->fullfilled(成功状态)
     9             reject('xxxx');//修改promise的状态pending----->rejected(失败状态)
    10         },1000)
    11     });
    12     promise.then((data) => {
    13         console.log('成功了。。。' + data);
    14     }, (error) => {
    15         console.log('失败了' + error);
    16     });
    17     console.log('2222');
    18 
    19 
    20     //定义一个请求news的方法
    21     function getNews(url) {
    22         //创建一个promise对象
    23         let promise = new Promise((resolve, reject) => {
    24             //初始化promise状态为pending
    25             //启动异步任务
    26             let request = new XMLHttpRequest();
    27             request.onreadystatechange = function () {
    28                 if(request.readyState === 4){
    29                     if(request.status === 200){
    30                         let news = request.response;
    31                         resolve(news);
    32                     }else{
    33                         reject('请求失败了。。。');
    34                     }
    35                 }
    36             };
    37             request.responseType = 'json';//设置返回的数据类型
    38             request.open("GET", url);//规定请求的方法,创建链接
    39             request.send();//发送
    40         })
    41         return promise;
    42     }
    43 
    44     getNews('http://localhost:3000/news?id=2')
    45             .then((news) => {
    46                 console.log(news);
    47                 document.write(JSON.stringify(news));
    48                 console.log('http://localhost:3000' + news.commentsUrl);
    49                 return getNews('http://localhost:3000' + news.commentsUrl);
    50             }, (error) => {
    51                 alert(error);
    52             })
    53             .then((comments) => {
    54                 console.log(comments);
    55                 document.write('<br><br><br><br><br>' + JSON.stringify(comments));
    56             }, (error) => {
    57                 alert(error);
    58             })
    View Code

    Symbol:

    前言:ES5中对象的属性名都是字符串,容易造成重名,污染环境
    Symbol:
    概念:ES6中的添加了一种原始数据类型symbol(已有的原始数据类型:String, Number, boolean, null, undefined, 对象)
    特点:
    1、Symbol属性对应的值是唯一的,解决命名冲突问题
    2、Symbol值不能与其他数据进行计算,包括同字符串拼串
    3、for in, for of遍历时不会遍历symbol属性。
    使用:
    1、调用Symbol函数得到symbol值
    let symbol = Symbol();
    let obj = {};
    obj[symbol] = 'hello';
    2、传参标识
    let symbol = Symbol('one');
    let symbol2 = Symbol('two');
    console.log(symbol);// Symbol('one')
    console.log(symbol2);// Symbol('two')
    3、内置Symbol值
    * 除了定义自己使用的Symbol值以外,ES6还提供了11个内置的Symbol值,指向语言内部使用的方法。
    - Symbol.iterator
    * 对象的Symbol.iterator属性,指向该对象的默认遍历器方法(后边讲)

     1    window.onload = function () {
     2       let symbol = Symbol();
     3       console.log(typeof symbol);
     4       console.log(symbol);
     5       
     6       // 用作对象的属性(唯一)
     7       let obj = {username: 'kobe', age: 39};
     8       obj[symbol] = 'hello';
     9       obj[symbol] = 'symbol';
    10       console.log(obj);
    11       for(let i in obj){
    12         console.log(i);
    13       }
    14     }
    View Code

    Iterator遍历器:

    概念: iterator是一种接口机制,为各种不同的数据结构提供统一的访问机制
    作用:
    1、为各种数据结构,提供一个统一的、简便的访问接口;
    2、使得数据结构的成员能够按某种次序排列
    3、ES6创造了一种新的遍历命令for...of循环,Iterator接口主要供for...of消费。
    工作原理:
    - 创建一个指针对象,指向数据结构的起始位置。
    - 第一次调用next方法,指针自动指向数据结构的第一个成员
    - 接下来不断调用next方法,指针会一直往后移动,直到指向最后一个成员
    - 每调用next方法返回的是一个包含value和done的对象,{value: 当前成员的值,done: 布尔值}
    * value表示当前成员的值,done对应的布尔值表示当前的数据的结构是否遍历结束。
    * 当遍历结束的时候返回的value值是undefined,done值为false
    原生具备iterator接口的数据(可用for of遍历)
    1、Array
    2、arguments
    3、set容器
    4、map容器
    5、String

     1     window.onload = function () {
     2         // 自定义iterator生成指针对象
     3         function mockIterator(arr) {
     4           let nextIndex = 0;
     5           return {
     6             next: function () {
     7               return nextIndex<arr.length?{value: arr[nextIndex++], done: false}:{value: undefined, done: true}
     8             }
     9           }
    10         }
    11 
    12         let arr = [1,2,3,4,5];
    13         let iteratorObj = mockIterator(arr);
    14         console.log(iteratorObj.next());
    15         console.log(iteratorObj.next());
    16         console.log(iteratorObj.next());
    17 
    18 
    19         // 使用解构赋值以及...三点运算符时会调用iterator接口
    20         let arr1 = [1,2,3,4,5];
    21         let [value1, ...arr2] = arr1;
    22         // yield*语句
    23         function* generatorObj() {
    24           yield '1'; // 可遍历数据,会自动调用iterator函数
    25           yield '3';
    26         }
    27         let Go = generatorObj();
    28         console.log(Go.next());
    29         console.log(Go.next());
    30         console.log(Go.next());
    31 
    32 
    33         // 原生测试  数组
    34         let arr3 = [1, 2, 'kobe', true];
    35         for(let i of arr3){
    36           console.log(i);
    37         }
    38         // 字符串  string
    39         let str = 'abcdefg';
    40         for(let item of str){
    41           console.log(item);
    42         }
    43         
    44 
    45       }
    View Code

    Generator函数

    概念:
    1、ES6提供的解决异步编程的方案之一
    2、Generator函数是一个状态机,内部封装了不同状态的数据,
    3、用来生成遍历器对象
    4、可暂停函数(惰性求值), yield可暂停,next方法可启动。每次返回的是yield后的表达式结果
    特点:
    1、function 与函数名之间有一个星号
    2、内部用yield表达式来定义不同的状态
    例如:
    function* generatorExample(){
    let result = yield 'hello'; // 状态值为hello
    yield 'generator'; // 状态值为generator
    }
    3、generator函数返回的是指针对象(接11章节里iterator),而不会执行函数内部逻辑
    4、调用next方法函数内部逻辑开始执行,遇到yield表达式停止,返回{value: yield后的表达式结果/undefined, done: false/true}
    5、再次调用next方法会从上一次停止时的yield处开始,直到最后
    6、yield语句返回结果通常为undefined, 当调用next方法时传参内容会作为启动时yield语句的返回值。

     1   // 小试牛刀
     2     function* generatorTest() {
     3       console.log('函数开始执行');
     4       yield 'hello';
     5       console.log('函数暂停后再次启动');
     6       yield 'generator';
     7     }
     8     // 生成遍历器对象
     9     let Gt = generatorTest();
    10     // 执行函数,遇到yield后即暂停
    11     console.log(Gt); // 遍历器对象
    12     let result = Gt.next(); // 函数执行,遇到yield暂停
    13     console.log(result); // {value: "hello", done: false}
    14     result = Gt.next(); // 函数再次启动
    15     console.log(result); // {value: 'generator', done: false}
    16     result = Gt.next();
    17     console.log(result); // {value: undefined, done: true}表示函数内部状态已经遍历完毕
    18 
    19     // 对象的Symbol.iterator属性;
    20     let myIterable = {};
    21     myIterable[Symbol.iterator] = function* () {
    22       yield 1;
    23       yield 2;
    24       yield 4;
    25     };
    26     for(let i of myIterable){
    27       console.log(i);
    28     }
    29     let obj = [...myIterable];
    30     console.log(obj);
    31 
    32     console.log('-------------------------------');
    33     // 案例练习
    34     /*
    35     * 需求:
    36     * 1、发送ajax请求获取新闻内容
    37     * 2、新闻内容获取成功后再次发送请求,获取对应的新闻评论内容
    38     * 3、新闻内容获取失败则不需要再次发送请求。
    39     * 
    40     * */ 
    41     function* sendXml() {
    42       // url为next传参进来的数据
    43      let url = yield getNews('http://localhost:3000/news?newsId=2');
    44       yield getNews(url);
    45     }
    46     function getNews(url) {
    47       $.get(url, function (data) {
    48         console.log(data);
    49         let commentsUrl = data.commentsUrl;
    50         let url = 'http://localhost:3000' + commentsUrl;
    51         // 当获取新闻内容成功,发送请求获取对应的评论内容
    52         // 调用next传参会作为上次暂停是yield的返回值
    53         sx.next(url);
    54       })
    55     }
    56 
    57 
    58     let sx = sendXml();
    59     // 发送请求获取新闻内容
    60     sx.next();
    View Code

    async函数:

    async函数(源自ES2017)
    概念: 真正意义上去解决异步回调的问题,同步流程表达异步操作
    本质: Generator的语法糖
    语法:
    async function foo(){
    await 异步操作;
    await 异步操作;
    }
    特点:
    1、不需要像Generator去调用next方法,遇到await等待,当前的异步操作完成就往下执行
    2、返回的总是Promise对象,可以用then方法进行下一步操作
    3、async取代Generator函数的星号*,await取代Generator的yield
    4、语意上更为明确,使用简单,经临床验证,暂时没有任何副作用

     1  async function timeout(ms) {
     2       return new Promise(resolve => {
     3         setTimeout(resolve, ms);
     4       })
     5     }
     6     
     7     async function asyncPrint(value, ms) {
     8       console.log('函数执行', new Date().toTimeString());
     9       await timeout(ms);
    10       console.log('延时时间', new Date().toTimeString());
    11       console.log(value);
    12     }
    13 
    14     console.log(asyncPrint('hello async', 2000));
    15     
    16     // await 
    17     async function awaitTest() {
    18       let result = await Promise.resolve('执行成功');
    19       console.log(result);
    20       let result2 = await Promise.reject('执行失败');
    21       console.log(result2);
    22       let result3 = await Promise.resolve('还想执行一次');// 执行不了
    23       console.log(result3);
    24     }
    25     awaitTest();
    26   
    27   
    28     // 案例演示
    29     async function sendXml(url) {
    30       return new Promise((resolve, reject) => {
    31         $.ajax({
    32           url,
    33           type: 'GET',
    34           success: data =>  resolve(data),
    35           error: error => reject(error)
    36         })
    37       })
    38     }
    39 
    40     async function getNews(url) {
    41       let result = await sendXml(url);
    42       let result2 = await sendXml(url);
    43       console.log(result, result2);
    44     }
    45     getNews('http://localhost:3000/news?id=2')
    View Code

    class:

    1. 通过class定义类/实现类的继承
    2. 在类中通过constructor定义构造方法
    3. 通过new来创建类的实例
    4. 通过extends来实现类的继承
    5. 通过super调用父类的构造方法
    6. 重写从父类中继承的一般方法

     1     class Person {
     2         //调用类的构造方法
     3         constructor(name, age){
     4             this.name = name;
     5             this.age = age;
     6 
     7         }
     8         //定义一般的方法
     9         showName(){
    10             console.log(this.name, this.age);
    11         }
    12     }
    13     let person = new Person('kobe', 39);
    14     console.log(person, person.showName());
    15 
    16     //定义一个子类
    17     class StrPerson extends Person{
    18         constructor(name, age, salary){
    19             super(name, age);//调用父类的构造方法
    20             this.salary = salary;
    21         }
    22         showName(){//在子类自身定义方法
    23             console.log(this.name, this.age, this.salary);
    24         }
    25     }
    26     let str = new StrPerson('weide', 38, 1000000000);
    27     console.log(str);
    28     str.showName();
    View Code
  • 相关阅读:
    ios启动画面
    不让精灵移除屏幕外 重写setPosition方法
    post和get请求方式的区别
    IOS开发之手势——UIGestureRecognizer 共存
    Xcode 中的GDB的命令
    [UIView beginAnimations:context:]与[UIView animateWithDuration:animations:]值得注意的一个区别
    应用图标 ICON
    cocos2d 1.01不能运行以前版本工程的问题
    SQL 中数据类型的转换 转
    SQL Server中sql语句执行时间
  • 原文地址:https://www.cnblogs.com/chenyanlong/p/10414470.html
Copyright © 2011-2022 走看看