0

So, I was just watching a tutorial from WebDevSimplified, and here is that piece of code that bothers me.

const failure1 = false;
const failure2 = false;

function callbackFunction(callback, errorCallback) {
    if (failure1) {
        errorCallback({
            name: 'Negative event1 occurred',
            message: ':('
        })
    } else if (failure2) {
        errorCallback({
            name: 'Negative event2 occurred',
            message: ':/'
        })
    } else {
        callback('Mission complete!')
    }
}

callbackFunction((message) => {
    console.log('Success: ' + message)
}, (error) => {
    console.log(error.name + ' ' + error.message)
})

So, why exactly is there a value assigned to the function parameters like this:

callback('Mission complete!')

Because according to every information I found on the internet I would do it like this:

callback = 'Mission complete'

Yet, when I do it this way, it's not working. I really feel super bad right now, because this seems very trivial and I cannot find any information about it. I would really appreciate it if someone would explain it to me.

PRMK
  • 15
  • 4

1 Answers1

1

No. The syntaxes have completely different meanings.


This assigns a value to callback:

callback = 'Mission complete'

This calls the function callback and passes an argument to it:

callback('Mission complete!')

The value will be assigned to the variable specified in the first argument of the function definition:

function callback(this_is_the_variable) {
     if (this_is_the_variable === 'Mission complete!') {
         // something
     }
}

(In your example this_is_the_variable is named message)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Still, that line code looks weird: errorCallback({ name: 'Negative event1 occurred', message: ':(' }) Can I pass an object as a parameter of a function like this? – PRMK Jun 09 '21 at 22:03
  • You can pass any kind of value. – Quentin Jun 09 '21 at 23:04