访问回调函数内的函数参数

huangapple go评论65阅读模式
英文:

Access parameters of a function inside a callback Function

问题

我正在练习回调函数,想知道如何在不使用 Promises 的情况下在回调函数内部访问函数的结果...

const mdn = (name) => {
    if (typeof name === 'string') {
        resolve()
    }
    else {
        reject()
    }
}

const resolve = function() {
    console.log(`Hello ${name}`)
}

const reject = function() {
    console.log(`invalid Name`)
}

mdn('ok')

如何在回调函数内部访问调用函数的结果?

英文:

i am practicing callback functions and i want to know how do i access the result of a function, indide of a callback function without using promises...

<!-- language: lang-html -->

 const mdn = (name) =&gt; {
	if(typeof name === &#39;string&#39;) {
		resolve()
  }
  else {
  	reject()
  }
}
 
 const resolve = function() {
 		console.log(`Hello ${name}`)
    }
    
 const reject = function() {
 		console.log(`invalid Name`)
 }

mdn(&#39;ok&#39;)

<!-- end snippet -->

How do i access the result of calling the function, inside the callback function itself ?

答案1

得分: 1

A callback function is one that is passed as an argument to a function which then calls it. You don't have any of those.

A function has access to the variables that are:

  • Defined inside it, including as arguments
  • Exist at the point where it is defined itself (unless they are shadowed).

Your variable (name) exists only in the scope of the mdn function.

resolve and reject are not defined inside mdn. They do not have access to name.


You can change where those functions are defined, so the name variable is in scope.

const mdn = (name) => {
  const resolve = function() {
    console.log(`Hello ${name}`)
  }

  const reject = function() {
    console.log(`invalid Name`)
  }
  if (typeof name === 'string') {
    resolve()
  } else {
    reject()
  }
}

mdn('ok')

You can pass the value of the variable as an argument to resolve and reject.

const mdn = (name) => {
  if (typeof name === 'string') {
    resolve(name)
  } else {
    reject(name)
  }
}

const resolve = function(name) {
  console.log(`Hello ${name}`)
}

const reject = function(name) {
  console.log(`invalid Name`)
}

mdn('ok')

Earlier I pointed out that your functions are not callbacks. Making them callback functions wouldn't make a difference to the variable scope.

英文:

A callback function is one that is passed as an argument to a function which then calls it. You don't have any of those.

A function has access to the variables that are:

  • Defined inside it, including as arguments
  • Exist at the point where it is defined itself (unless they are shadowed).

Your variable (name) exists only in the scope of the mdn function.

resolve and reject are not defined inside mdn. They do not have access to name.


You can change where those functions are defined, so the name variable is in scope.

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

const mdn = (name) =&gt; {
  const resolve = function() {
    console.log(`Hello ${name}`)
  }

  const reject = function() {
    console.log(`invalid Name`)
  }
  if (typeof name === &#39;string&#39;) {
    resolve()
  } else {
    reject()
  }
}


mdn(&#39;ok&#39;)

<!-- end snippet -->

You can pass the value of the variable as an argument to resolve and reject.

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

const mdn = (name) =&gt; {
  if (typeof name === &#39;string&#39;) {
    resolve(name)
  } else {
    reject(name)
  }
}

const resolve = function(name) {
  console.log(`Hello ${name}`)
}

const reject = function(name) {
  console.log(`invalid Name`)
}

mdn(&#39;ok&#39;)

<!-- end snippet -->


Earlier I pointed out that your functions are not callbacks. Making them callback functions wouldn't make a difference to the variable scope.

答案2

得分: 0

"Promise"和"callback"实际上与此无关

您需要将参数传递给"resolve":

const mdn = (name) => {
    if(typeof name === 'string') {
        resolve(name)
    }
    else {
        reject()
    }
}

const resolve = function(name) {
    console.log(`Hello ${name}`)
}

const reject = function() {
    console.log(`invalid Name`)
}

mdn('ok')
英文:

actually Promise, callback has nothing to do with this

you need to pass parameters to resolve:

const mdn = (name) =&gt; {
    if(typeof name === &#39;string&#39;) {
        resolve(name)
  }
  else {
    reject()
  }
}
 
 const resolve = function(name) {
        console.log(`Hello ${name}`)
    }
    
 const reject = function() {
        console.log(`invalid Name`)
 }

mdn(&#39;ok&#39;)

huangapple
  • 本文由 发表于 2023年2月27日 09:52:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定