Miawa
Miawa

Reputation: 312

apply() / call() vs [function]() in jquery

What is more efficient in jquery:

if ($.isFunction(func_name)
    $.apply(func_name, [param, etc])

or

if ($.isFunction(func_name)
    func_name(param, etc)

What are the advantages to call $.apply() or $.call() over calling directly the object if it is a function as a callback.

Thanks

Upvotes: 1

Views: 4592

Answers (1)

pdoherty926
pdoherty926

Reputation: 10409

Unless you're manipulating contexts, there's no reason to use call or apply or the equivalent jQuery helpers.

Also, you don't need to use jQuery to test if a function exists.

function foo() { alert('foo') }

if (foo && typeof foo === 'function') {
    foo();
}

Fiddle

Upvotes: 3

Related Questions