Skip to content

ES6

javascript
const numbers = [1,2,3,4]
const double = numbers.map(function(number){ return number *2; });
const double = numbers.map(number => { return number *2; });
const double = numbers.map(number => number *2);//隐式返回
const double = numbers.map((number , i) => { return`${i}:{number *2}`; });
const double = numbers.map(() => { return 'hello'; });

//下面两个等价
function greet(name){...}
const great = name => {...}

alert(`hello ${double}`)

//箭头函数的this继承父级
const Jelly = {//对象
    name: 'Jelly',
    hobbies:['Coding','Sleeping'],
    printHobbies: function(){
        consle.log(this);
        this.hobbies.map(hobby => {
            console.log(`${this.name} loves ${hobby}`);//箭头函数this继承父级
        })    
    }
}
Jelly.printHobbies();
javascript
//ES6设置默认值
function multiply(a = 5,b = 3){
    return a*b;
}

//老版设置默认值
function multiply(a,b){
    a = a || 5;//5为默认值
    b = b || 3;//3为默认值
    return a*b;
}

multiply(undefined,2)
javascript
const Person = function(name, points){
    this.name = name;    //不能使用箭头函数
    this.points= points;
}

const Jelly = new Person('Jelly',5);


Person.prototype.updatePoints = function(){ //prototype原型方法
    this.points++;
    console.log(this.points);
}


const button = document.querySelector('.zoom');
button.addEventListener('click',function(){
    this.classList.add('in');
    setTimeout(() => {
        this.classList.remove('in'); //2s后移除class'in',this指定父级
    },2000)
}
javascript
\\魔法字符串 Template Strings/literals
const sentence = person+' is 25';
const sentence = `${person} is 25`;


const template =[
'<div class ="great">''
 '<p>Hello</p>''
'</div>''
].join('')

const template =`
<div class ="great">
 <p>Hello</p>
</div>
`.trim()//trim删除空格

Released under the MIT License.