了解下面的javascript函数和流程
了解下面的javascript函数和流程,javascript,function,Javascript,Function,makeAddFunction()是一个返回新创建函数的函数。结果取决于传递给makeAddFunction()的amount参数 编写makeAddFunction(5)时,解释器会记住给定上下文中的amount,因此生成的函数相当于: function makeAddFunction(amount) { function add(number) {/* We define a function add() that takes 1 argument
makeAddFunction()
是一个返回新创建函数的函数。结果取决于传递给makeAddFunction()
的amount
参数
编写makeAddFunction(5)
时,解释器会记住给定上下文中的amount
,因此生成的函数相当于:
function makeAddFunction(amount) {
function add(number) {/*
We define a function add() that takes 1 argument
return number + amount;//The function returns our "number" argument
plus some "amount", determined by what's passed to makeAddFunction().
At this stage, you can think of "amount" as a fixed number.
*/
}
return add;//We return the add() function, which takes the "number" argument
}
var addTwo = makeAddFunction(2);/*We create a function that takes "number" and
returns "number" + 2. The returned function is exactly the same as:
function(number){return number + 2;}
*/
var addFive = makeAddFunction(5);/*We create a function that takes "number" and
returns "number" + 5. The returned function is exactly the same as:
function(number){return number + 2;)
*/
show(addTwo(1) + addFive(1));//The result is (1 + 2) + (1 + 5) = 9
有一些javascript代码。你有什么问题吗?@AlexWayne你能逐行解释代码吗?我真的很困惑。哪一行让你困惑?你得到了哪一部分,你没有得到哪一部分?编辑您的问题并添加一些文本,更详细地描述您希望从答案中得到什么。试着理解它,至少是一点,然后问一个关于你没有理解的特定部分的问题。@AlexWayne,主要是,对最后三行感到困惑。谢谢:)除了这三行之外,我什么都懂了。addTwo和addFive函数如何知道做“number”+2;我编辑了答案。现在应该清楚了,谢谢。是的。我感谢你详细解释。我是新来的。所以return关键字和在屏幕上打印是一样的吗?比如console.log?或者return只获取值并保存它。你推荐的关于如何像软件工程师一样思考的任何材料。开始编码前要做什么?什么都可以。
function makeAddFunction(amount) {
function add(number) {/*
We define a function add() that takes 1 argument
return number + amount;//The function returns our "number" argument
plus some "amount", determined by what's passed to makeAddFunction().
At this stage, you can think of "amount" as a fixed number.
*/
}
return add;//We return the add() function, which takes the "number" argument
}
var addTwo = makeAddFunction(2);/*We create a function that takes "number" and
returns "number" + 2. The returned function is exactly the same as:
function(number){return number + 2;}
*/
var addFive = makeAddFunction(5);/*We create a function that takes "number" and
returns "number" + 5. The returned function is exactly the same as:
function(number){return number + 2;)
*/
show(addTwo(1) + addFive(1));//The result is (1 + 2) + (1 + 5) = 9
function add(number){
return number + 5;
}