Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgqfrms-GitHub/43f021cd194c792c9cac504a071f741a to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/43f021cd194c792c9cac504a071f741a to your computer and use it in GitHub Desktop.
ES6 arrow-functions in deepth

ES6 arrow-functions in deepth

https://wesbos.com/arrow-functions/

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions#

const implicit = (a) => {
    a = a + 1;
    return a;
}


const implicitX = x => x = x + 1;


const implicit = (a) => {
    a = a + 1;
    let b = a * a;
    return b;
}



const implicitY = y => y = (y + 1) * (y + 1);

implicitY(2);
// 9

/* 当删除大括号时,它将是一个隐式的返回值,这意味着我们不需要指定我们返回*/




const implicitA = y => {y = (y + 1) * (y + 1);};

implicitA(2);
// undefined

/* 当有大括号时,我们需要指定返回一个显示的返回,*/




https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions#


// 如果箭头函数 有多个参数, 必须使用 ()圆括号:

const multi_params = (a, b) => {
    let sum = a + b;
    console.log(sum);
};


multi_params(1, 2);
// 3



// 如果箭头函数 无参数, 必须使用 ()圆括号或者 _下划线:


_ => { statements; }

const multi_X = _ => {
    let sum = a + b;
    console.log(sum);
};


multi_X(1, 2);
// Uncaught ReferenceError: a is not defined