英文:
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) => {
if(typeof name === 'string') {
resolve()
}
else {
reject()
}
}
const resolve = function() {
console.log(`Hello ${name}`)
}
const reject = function() {
console.log(`invalid Name`)
}
mdn('ok')
<!-- 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) => {
const resolve = function() {
console.log(`Hello ${name}`)
}
const reject = function() {
console.log(`invalid Name`)
}
if (typeof name === 'string') {
resolve()
} else {
reject()
}
}
mdn('ok')
<!-- 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) => {
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')
<!-- 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) => {
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论