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
@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

<ul id="parent-list">
    <li id="post-1">Item 1</li>
    <li id="post-2">Item 2</li>
    <li id="post-3">Item 3</li>
</ul>
// Get the element, add a click listener...
document.getElementById("parent-list").addEventListener("click", function(e) {
    // e.target is the clicked element!
    // If it was a list item
    if(e.target && e.target.nodeName == "LI") {
        // List item found!  Output the ID!
        console.log("List item ", e.target.id.replace("post-", ""), " was clicked!");
    }
});



// Get the parent DIV, add click listener...
document.getElementById("myDiv").addEventListener("click",function(e) {
    // e.target was the clicked element
    if (e.target && e.target.matches("a.classA")) {
        console.log("Anchor element clicked!");
    }
});

@xgqfrms-GitHub
Copy link
Author

No Arguments with Arrow Functions & Arrow Functions are Always Anonymous Functions

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

arrow-function no-parameters all-anonymous-function
instanceof constructor array vs object
js-script-async-defer timeline
let void 0
let hoisting temporal dead zone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment