简体   繁体   English

Javascript箭头函数中的参数名称

[英]Parameter names in Javascript arrow functions

I was reading up on Promise types for Javascript, and I am stumped by the purpose of div in the .then(...) 我正在阅读Java的Promise类型,但在.then(...)中div的目的使我感到.then(...)

 function go() { showCircle(150, 150, 100).then(div => { div.classList.add('message-ball'); div.append("Hello, world!"); }); } function showCircle(cx, cy, radius) { let div = document.createElement('div'); div.style.width = 0; div.style.height = 0; div.style.left = cx + 'px'; div.style.top = cy + 'px'; div.className = 'circle'; document.body.append(div); return new Promise(resolve => { setTimeout(() => { div.style.width = radius * 2 + 'px'; div.style.height = radius * 2 + 'px'; div.addEventListener('transitionend', function handler() { div.removeEventListener('transitionend', handler); resolve(div); }); }, 0); }) } 
 .message-ball { font-size: 20px; line-height: 200px; text-align: center; } .circle { transition-property: width, height, margin-left, margin-top; transition-duration: 2s; position: fixed; transform: translateX(-50%) translateY(-50%); background-color: red; border-radius: 50%; } 
 <button onclick="go()">Click me</button> 

I would like to understand the significance of "div" (though I tried renaming it, and the .then(...) works fine, however an anonymous function () does not). 我想了解“ div”的重要性(尽管我尝试重命名它,并且.then(...)可以正常工作,但是匿名function ()却不行)。

'div' here in your code showCircle(150, 150, 100).then(div => { } ); 代码中的' showCircle(150, 150, 100).then(div => { } ); is the data that is returned from your promise function ShowCircle. 是您的诺言函数ShowCircle返回的数据。 In general, in an arrow function, anything on the left hand side of your lambda expression (=>) is your input paramaters for the function. 通常,在箭头函数中,lambda表达式(=>)左侧的任何内容都是该函数的输入参数。

div => {
//doSomething here
}

is

function nameFunction(div){
//doSomething here
    }

In a promise then case, the input supplied to the function is the return value from the promise. 在一个承诺然后情况下,供给到该函数的输入是来自承诺的返回值。 Here the param is named as div and holds the return value from ShowCircle(). 在这里,参数被命名为div,并保存ShowCircle()的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM