Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function in JavaScript without parentheses

Tags:

javascript

Is there a way in JavaScript to call a function without parentheses?

For example in jQuery:

$('#wrap').text("asdf"); will work and so will $.ajax(ajaxOptions);

I'm mapping a function (class) to window.$ that has a set of functions I want to be able to call with or without parentheses. Like jQuery.

Here's a code example:

function Test(asdf) {
  this.test = function(testVar) { return testVar + ' asdf'; }
}

And I map Test() to $:

window.$ = new Test();

I have to call the function (class) like this:

$('asfd').test('ASDF');

But I want to be able to call it like this:

$.test('asdf');
like image 328
errorhandler Avatar asked Dec 28 '25 16:12

errorhandler


2 Answers

JavaScript is extremely flexible since objects are basically a map and can contain any number of key value pairs, where the value itself can be an object and thus you get nesting. Another interesting aspect of JavaScript is that functions are themselves first class objects, so in a key value pair, you can have a function as a value.

To get something like jQuery then becomes very simple. You start off with a function (constructor function, or class if you will),

function myFunction() {
    // do some awesome web 2.0 stuff
}

Since myfunction is a function and also an object, so you attach key value pairs to it as well which is what jQuery does. To verify that myFunction is also an object, do an instanceof check:

myFunction instanceof Function; // true
myFunction instanceof Object; // true

So we can add properties to a function which could be simple values, or functions themselves.

// adding a simple property that holds a number
myFunction.firstPrime = 2;

// adding a function itself as a property to myFunction
myFunction.isPrime = function(number) {
    // do some magic to determine if number is prime
};

But if that is not your question, and you really want to know if you can literally call a function without using parentheses, then the answer is yes, you can, using the additions in ECMAScript 5th ed.

Here's an example of calling a function without using parentheses using Object.defineProperty and defining it as a getter:

var o = {};

Object.defineProperty(o, "helloWorld", {
    get: function() {
        alert("hello world");
    },
    set: undefined
});

o.helloWorld; // will alert "hello world"

Try this example in Chrome or Safari, or Firefox nightly.

like image 182
Anurag Avatar answered Dec 31 '25 06:12

Anurag


new


You can call a function using new. The twist is, within the function this becomes an object being constructed by the function instead of whatever it would normally be.

function test () { alert('it worked'); }
...
new test; // alerts "it worked"

call and apply


You can use call or apply to call a function indirectly. You'll still need parentheses (unless you use new), but you won't be directly invoking the function with parentheses.

like image 37
Dagg Nabbit Avatar answered Dec 31 '25 06:12

Dagg Nabbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!