ES6部分方法点评(二)

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了ES6部分方法点评(二)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

template string

template string(模板字符串),至ES6,javascript终于也能直接往字符串里插变量了。这用途嘛,说大不大,说小也不小;虽说不能实现比较复杂的例如if/for等语句就不能说是一个完整的模板引擎,但起码以后拼字符串就不用老写连接符+了不是?

let name = 'guoyongfeng';
let age = 18;

console.log(`${name} want to drink ${age}`)

Default(函数默认参数

喜大普奔!javascript终于能像其它语言一样在语言层面给形参设默认值了:

function f(x, y=12) {
  // y is 12 if not passed (or passed as undefined)
  return x + y;
}
f(3) == 15

class, extends, super

作为一个从PHP起跑的码农,这仨语法糖我真的是不得不吃。一直以来,javascript的面向对象一般都是靠prototype,但毕竟跟其它语言中的class还是相差甚远的(当然硬要实现class也行,就是特麻烦),现在ES6终于从语言层面实现class了,鼓掌!!

class Animal {
    constructor(){
        this.type = 'animal'
    }
    says(say){
        console.log(this.type + ' says ' + say)
    }
}

let animal = new Animal()
animal.says('hello') //animal says hello

class Cat extends Animal {
    constructor(){
        super()
        this.type = 'cat'
    }
}

let cat = new Cat()
cat.says('hello') //cat says hello

Object.assign

这实际上就是jquery/zepto提供的extend方法,即把多个object合并到一起,这下又多了一个抛弃jquery/zepto的理由了:

var target = { a: 1 };

var source1 = { b: 2 };
var source2 = { c: 3 };

Object.assign(target, source1, source2);
console.log(target); // {a:1, b:2, c:3}

脚本宝典总结

以上是脚本宝典为你收集整理的ES6部分方法点评(二)全部内容,希望文章能够帮你解决ES6部分方法点评(二)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: