Mankament Gra Mankament Gra - 1 month ago 7
Javascript Question

functions an immediate strange behavior error in console.log

I have code:

function fn(ob)
{
console.log(ob.name)
}

fn({name:"myName"})


(function(text){

console.log(text)

})("Error")


But when i run this i get TypeError: fn(...) is not a function
[Learn More] on console. Why?

Answer

You need to separate between the call to the fn function and the definition (and call) to the anonymous function.
You can do this using the ; char after the call to the fn function:

function fn(t) {
  console.log(t.name)
}
fn({name:"myName"});

(function(text){
  console.log(text)
})("Error")

Otherwise your code is actually:

fn({name:"myName"})(function(text){
    console.log(text)
})("Error")

And this is the error you got.