javascript的数据结构与算法--使用let和const声明变量;模板字面量;解构;展开运算符;箭头函数;类;模块

检查在哥哥浏览器中哪些特性可用 :

https://kangax.github.io/compat-table/es6

https://kangax.github.io/compat-table/es2016plus/

firefox中是默认开启支持ES功能的实现,在谷歌中,可访问chrome://flags/#enable-javascript-harmony开启Experimental JavaScript

JavaScript源代码编译器:Babel.js :https://babeljs.io/repl

es6常用功能:使用let和const声明变量;模板字面量;解构;展开运算符;箭头函数;类;模块

   还包括 迭代器 类型数组 Set 、 Map、 WeakSet 、WeakMap 、 尾调用 、for...of 、Symbol、 includes  、尾逗号 、字符串补全 、 静态对象方法等,详细查询 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/

 1.使用let和const声明变量

     let name = 'Owen'; // name 在同一个作用域不能重复声明,但var可以

    const  PI=3.141593 ; // 不能给 PI重新赋值 

   const obj ={

     name ;‘Owen’   

}

 obj.name = 'Owen Two'; // 正确

 

 const obj ={    // 错误不能重新指定对象的引用

     name ;‘Owen Two’   

}

 

2.模板字面量

 const book={

    name:"学习js技术使人进步" 

}

console.log('你正在'+book.name+‘.\n 这是新的一行\n 这也是’) // es5写法

console.log(`你正在${book.name}

这是新的一行

这也是`) ; //模板字符串的写法

 

3.箭头函数

  const fun = r => { // 省去 function关键字

      const PI=3.14;

        const area = PI*r*r

       return area;

}

consr fun2= r => 3.14*r*r; //只有简单的一句时,function和return 都省去

const fun3 = () => console.log('无参数传递')

 

const  sum= (x=1,y=2,z=3) => {

  return x+y+z;

}

// 以下es5的两种写法

function (x,y,z){

        if(x === undefined)   x= 1;

        if(y === undefined)   y= 2;

        if(z === undefined)   z= 3;

   return x+y+z;

}

function (x,y,z){

   var x = arguments.length>0 && arguments[0] !==undefined ? argument[0] : 1;

   var y = arguments.length>0 && arguments[0] !==undefined ? argument[0] : 2;

   var z = arguments.length>0 && arguments[0] !==undefined ? argument[0] : 3;

return  x+y+z;

}

 

4.展开运算符: ...

   let params = [1,2,3] ;

    sum(...params)    <=>  sum.apply(undefined,parms)

     const fun = (x,y,...rest) =>{  // 剩余参数
  return (x+y)*rest.length;
}
fun(1,2,'hello',true,7)
console.log(fun(1,2,'hello',true,7))

// es5 写法

 var fun = function fun(x, y) {
  return (x + y) * (arguments.length <= 2 ? 0 : arguments.length - 2);
};

 

var fun = function fun(x, y) {

    var rest = Array.prototype.slice.call(arguments,2)

    return (x+y)*rest.length;

}

 

5.解构

      let [x,y]=['a','b'] ;// 数组的解构

             等价: let x = 'a' ,let y = 'b'

      [x,y]=[y,x];

         等价:temp =x; x=y; y=temp;

// 属性简写 : 对象解构的一种方式

let  [x,y] = ['a','b'] ;

let obj = {x,y} 

console.log(obj);//{x:'a',y:'b'}

//简写方法名

const hello = {

   name:'Owen',

  say(){

              console.log('say hello') 

     }

}

const hello = {

   name:'Owen',

   say :function say()a{

     console.log('say hello') 

    }

}

 

 

 

6.类
function Book(title,pages,isbn){
  this.title=title;
  this.pages=pages;
  this.isbn=isbn
}
Book.prototype.printIsbn=function(){
console.log(this.isbn);
}

 // es6写法
class Book{
   constructor(title,pages,isbn){
      this.title=title;
      this.pages=pages;
      this.isbn=isbn
    }
  printIsbn(){
    console.log(this.isbn);
  }
}

let book = new Book('营养学',800,false);
book.title="阳光书院";
book.pages=90;
book.isbn=true;
book.printIsbn()

// 继承

class ITBook extends Book{

   constructor(title,pages,isbn,technology){

        super(title,pages,isbn);

         this.technology=technology

   }

   printTechnology(){

    console.log(this.technology)

   }

}

let  itBook= new ITBook('学习js技术','200','1234567890','javascript');

console.log(itBook.title);//  '学习js技术'

console.log(itBook.printTechnology()) ;// 'javascript'

// 使用属性存储器 get()    set(value)

 class Person{

   constructor(name){

     this._name = name;

     }

   get name(){

       return this._name;

   }

  set name(value){

     this._name=value;

    }

}

 let lotChar = new Person('Owen');

 console.log(lotChar.name); // Owen

 lotChar.name="Owen LL";

 console.log(lotChar.name); // Owen LL

 lotChar._name = 'Sam' ;

console.log(lotChar.name); // Sam

 

7.乘方运算符 **

const area = 3.14*r*r;

const area = 3.14*Math.pow(r,2);

const area = 3.14*(r**2)

 

8.模块

 Node.js 使用require语句(CommonsJS模块) 

 JavaScript模块化标准  异步模块定义AMD模块)RequireJS是AMD最流行的实现

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值