Michael Zaporozhets
Michael Zaporozhets

Reputation: 24536

Immediately Called Functions are 'undefined'

I have the following:

var catCreyz = function() { console.log('maaaow'); }.call()

and when I test its type:

typeof catCreyz

The result returned is:

undefined

Why?

Upvotes: 1

Views: 94

Answers (2)

Steven Wexler
Steven Wexler

Reputation: 17279

Function.prototype.call returns what the function being called returns. Your function returns nothing

var catCreyz = function() { console.log('maaaow'); }.call()
//logs undefined
console.log(catCreyz);

var result = function() { return "foo"; }.call()
//logs foo
console.log(result);

var myFunc = function() { console.log('maaaow'); }
//logs the function
console.log(myFunc);

Upvotes: 4

BeWarned
BeWarned

Reputation: 2338

Call executes the function, and you're missing a return statement in your function, Javascript does not return the last expression like other languages. You must explicitly return a value, else you get undefined.

If you want an instance of catCreyz, then take the call() off the expression.

Upvotes: 1

Related Questions