Mankament Gra
Mankament Gra

Reputation: 147

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?

Upvotes: 3

Views: 34

Answers (1)

Dekel
Dekel

Reputation: 62566

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.

Upvotes: 3

Related Questions