了解JavaScript回调和形式参数 [英] Understanding javascript callbacks and formal parameters

查看:66
本文介绍了了解JavaScript回调和形式参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个我有些困惑的问题的例子。



函数一调用函数二,并向其传递具有形式参数的回调。



我的答案:

 函数one(){
函数二(回调)
{回调(参数1,参数2)}
}

请更正,如果这是错误的



问题:



a)哪个函数调用回调?



函数两个调用回调函数



b)哪个函数提供了回调的实际参数?



不确定



c)为什么要提供回调参数?



<不确定>



d)如果您在不提供回调的情况下进行异步通话,您会放弃什么?



不确定



e)异步调用中的被调用函数是否可以包含同步调用?



不确定

解决方案

解剖优先



JavaScript可能会令人困惑,因为存在各种表示函数的方式 –出于所有目的和目的,可以将以下功能视为相同

  //命名函数
函数myfunc( param1,param2){
return returnValue
}

//函数文字语法
const myfunc =函数(param1,param2){
return returnValue
}

//具有显式返回的箭头函数(请注意{...})
const myfunc =(param1,param2)=> {
return returnValue
}

//具有隐式return
的箭头函数const myfunc =(param1,param2)=> returnValue

上面您可以看到具有参数的功能 –在下面,您将见函数调用具有参数

  //调用带有0个参数的myfunc 
myfunc()

//用1个参数调用myfunc
myfunc(arg1)

//用2个参数调用myfunc
myfunc(arg1,arg2)






它们只是功能


a)哪个函数调用 callback 函数?


所以无论我们在哪里看到 arguments 提供的函数,我们都知道我们正在处理调用 –要回答 A ,只需看一下呼叫的封闭功能

  function someFunc( param1,param2){
//执行
const结果= param1 * 2

//以结果
调用的param2 param2(结果)
}

嘿, param2(结果)是函数 call 。我们看一下封闭函数,发现 someFunc 是调用它的那个


b)哪个函数提供 callback 函数的实际参数?


听起来听起来很傻吗?就像在问鲍比的名字是谁的? Bobby ,当然。您可能想问一下,哪个函数提供了参数-在这种情况下,答案将是 someFunc -它提供了结果参数


c)为什么要提供回调函数参数?


一旦我们停止调用函数回调 ,就会立即得到回答–回调是 function ,而不是相反。我们给函数参数,以便我们可以影响函数的行为


d)当您进行 n'异步'调用而没有提供 callback 函数时?


不必担心异步函数与同步函数的调用;只是函数调用。函数调用发生在不同的时间,但是在某些时候,调用将使用提供的参数评估函数的主体并返回结果; 未定义或其他方式



在大多数情况下,您会放弃对结果执行任何有用的功能的能力–再看一下我们的函数:如果我们不提供 param2 ,则 result 就没用

 函数someFunc(param1,param2){
//做某事
const结果= param1 * 2

//以结果
调用的param2 param2(结果)
}

但是,当然并非总是如此。要了解原因,我们必须首先解决您的问题,


d)


有一个愚蠢的问题吧?但是回调函数没有什么不同–它们只是像其他任何东西一样的普通值



下面我们有 someFunc 具有简单命名为 callback someFunc 的函数参数会产生有效结果指定的回调–因此, D 的答案是:您什么也不放弃,明确地



 函数someFunc(x,callback){if(callback)返回callback(x * 10)else return x * 2} // with callbacksomeFunc(5,console .log)// 50 //没有console.log(someFunc(5))// 10  


e) n异步调用中的被调用函数是否可以包含同步调用?


是的,函数可以调用其他函数。 javascript中的同步和异步这一概念在历史上是一种心理构造,因此许多人对此有不同的看法。除此之外,较新版本的JS具有 async 关键字,该关键字将隐式将函数的返回值转换为Promise



为进一步说明这一点,请观察 doSomething –是异步还是同步?



  //同步函数const mult =(x,y)=> x * y //同步函数const double =(x)=> mult(x,2)//具有回调的函数const doSomething =(x,callback)=> callback(x)//是异步吗?doSomething(5,x => {const result = double(x)console.log(result)// 10})//还是同步吗?console.log(doSomething( 5,double))// 10  






从这里去哪里



实际上, 对异步的理解更为统一现在的JavaScript –尤其是Promises的标准化和广泛采用。 ES2017也添加了新的控制语法 async / await 。 回调模式基本上已经死了,即使是Node的主导力量,即流行的 Node样式(错误优先)回调也已经承认支持异步程序的基于Promised的接口。



这并不是说高阶函数(带有函数参数的函数或返回其他函数的函数)没有使用-您将看到函数式程序员挥舞着他们的 Array.prototype.map Array.prototype.reduce 到处都是



但是回调功能非常强大-尤其是在以不同于我们上面看到的方式表示时。 连续传递样式是一种我们通过 continuation 传递的样式>(函数的另一个奇特名称),用作程序的下一步。我在续集主题上写了很多东西。如果您觉得这些东西有趣,请继续阅读。您将学到关于调用堆栈,递归原理,函子,单子函数等各种奇妙的东西!



  const cont = x => k => k(x)const double = x =>连续(x * 2)const三元= x => cont(x * 3)double(2)(console.log)// 4double(2)(double)(console.log)// 8double(2)(double)(double)(console.log)// 16double( 2)(double)(double)(double)(console.log)// 32double(2)(triple)(console.log)// 12  


Here is an example of a question that I am slightly confused about.

Function one calls function two, and passes it a callback that has a formal parameter.

My answer:

function one(){
function two(callback) 
{callback (param1, param2)}
}

please correct, if this is wrong

Questions:

a) Which function calls the callback?

function two calls the callback function

b) Which function supplies the actual parameter of the callback?

unsure

c) Why give a callback parameters?

unsure

d) What are you giving up when you have an 'asynchronous' call without supplying a callback?

unsure

e) Can the called function in an asynchronous call ever contain synchronous calls?

unsure

解决方案

anatomy first

JavaScript can be confusing because there's a variety of ways to express functions – for all intents and purposes, the functions below can be considered identical

// named function
function myfunc (param1, param2) {
  return returnValue
}

// function literal syntax
const myfunc = function (param1, param2) {
  return returnValue
}

// arrow function with explicit return (note { ... })
const myfunc = (param1, param2) => {
  return returnValue
}

// arrow function with implicit return
const myfunc = (param1, param2) => returnValue

Above you can see functions have parameters – below, you'll see function calls have arguments

// call myfunc with 0 arguments
myfunc ()

// call myfunc with 1 argument
myfunc (arg1)

// call myfunc with 2 arguments
myfunc (arg1, arg2)


they're just functions

a) Which function calls the callback function?

So wherever we see a function supplied with arguments, we know we're dealing with a call – to answer A, just look to see the enclosing function of the call

function someFunc (param1, param2) {
  // do something
  const result = param1 * 2

  // param2 called with result
  param2 (result)
}

Hey look, param2 (result) is a function call. We look to the enclosing function to see that someFunc is the one that called it

b) Which function supplies the actual parameter of the callback function?

Well that sounds silly doesn't it? That's like asking "Who does Bobby's name belong to?" Bobby, of course. You probably meant to ask, which function supplies the argument – in which case, that answer would be someFunc – it supplies result argument

c) Why give a callback function parameters?

This is instantly answered as soon as we stop calling functions a "callback" – a callback is a function, not the other way around. We give functions parameters so that we can affect the behaviour of the function

d) What are you giving up when you have an 'asynchronous' call without supplying a callback function?

Don't worry about asynchronous vs synchronous function calls; there's just function calls. Function calls happen at different times, but at some point the call will evaluate the function's body with the supplied arguments and return the result; undefined or otherwise

In most cases, you "give up" the ability to do anything useful with the result – look at our function again: if we don't supply param2, result just goes unused

function someFunc (param1, param2) {
  // do something
  const result = param1 * 2

  // param2 called with result
  param2 (result)
}

But of course that's not always true. To understand why, we have to first fix your question tho

d) What are you giving up when you have a call without supplying an argument?

Kind of a silly question right? But it's no different for callbacks functions – they're just ordinary values like anything else

Below we have someFunc with a function parameter naïvely named callbacksomeFunc produces a "valid" result with or without the callback specified – so the answer for D is: you give up nothing, explicitly

function someFunc (x, callback) {
  if (callback)
    return callback (x * 10)
  else
    return x * 2
}

// with the callback
someFunc (5, console.log) // 50

// without
console.log (someFunc (5)) // 10

e) Can the called function in an asynchronous call ever contain synchronous calls?

Yes, functions can call other functions. This idea of "sync" and "async" in javascript is historically a mental construct and therefore a lot of people have differing opinions on it. To add to this, newer versions of JS have an async keyword which will implicitly convert your function's return value to a Promise

To further illustrate this, observe doSomething – is it "asynchronous" or is it "synchronous" ?

// "synchronous" function
const mult = (x,y) => x * y

// "synchronous" function
const double = (x) => mult (x, 2)
  
// function with "callback"
const doSomething = (x, callback) =>
  callback (x)

// is it async ?
doSomething (5, x => {
  const result = double (x)
  console.log (result) // 10
})

// or is it sync ?
console.log (doSomething (5, double)) // 10


where to go from here

There is actually a more uniform understanding of asynchrony in JavaScript now – especially with the standardization and widespread adoption of Promises. ES2017 adds new control syntax async/await too. "Callback" patterns are mostly dead – even the dominant force that was Node with it's popular "Node-style (error first) callbacks" has conceded to support Promised-based interfaces for asynchronous programs

That's not to say higher-order functions (functions with function parameters, or functions that return other functions) are without their uses – you'll see the functional programmers waving their Array.prototype.map and Array.prototype.reduce all over the place

But this idea of "callbacks" can be extremely powerful – especially when represented in ways other than we've seen above. Continuation-passing style is a style where we pass a continuation (another fancy name for a function) that serves as the "next" step of our program. I've written a lot on the subject of continuations. If you find this stuff interesting, keep reading. You'll learn amazing things about the call stack, principles of recursion, functors, monads, all sorts of things !

const cont = x => k => k (x)

const double = x => cont (x * 2)

const triple = x => cont (x * 3)

double (2) (console.log)                            // 4
double (2) (double) (console.log)                   // 8
double (2) (double) (double) (console.log)          // 16
double (2) (double) (double) (double) (console.log) // 32
double (2) (triple) (console.log)                   // 12

这篇关于了解JavaScript回调和形式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆