JavaScript function call without parentheses

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 map a function (class) to window.$ , Which has a set of functions that I want to have in order to call with or without parentheses. Like jQuery.

Here is a sample code:

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

And I map Test() to $ :

 window.$ = new Test(); 

I need to call a function (class) as follows:

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

But I want it to be called like this:

 $.test('asdf'); 
+2
source share
7 answers

You can try something like this

 var test = { method1 : function(bar) { // do stuff here alert(bar); }, method2 : function(bar) { // do stuff here alert(bar); } }; test.method1("foo"); test.method2("fooo"); 
+1
source

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 therefore you get nesting. Another interesting aspect of JavaScript is that the functions themselves are first class objects, so you can have a function as a value in a pair of key values.

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

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

Since myfunction is a function and also an object, so you also attach key value pairs to it, 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 

Thus, we can add properties to a function, which can be a simple value or the 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 this 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 is an example of calling a function without using parentheses using Object.defineProperty and defining it as the recipient:

 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 at night.

+37
source

new


You can call the function using new . The twist in the this function becomes an object that is built by the function, and not what 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 still need parentheses (unless you use new ), but you will not directly refer to the function with parentheses.

+4
source
 function message() {return "Hello crazy world!"; } Function.prototype.toString = function() { return this.call(this); }; alert(message); 
+4
source

The difference in these examples is that .text() is a function in jQuery objects (instances of them, element wrappers), on $.fn (jQuery prototype shortcut), so it is specifically designed for jQuery objects.

$.ajaxSetup() is a method in the jQuery object / variable itself.

If you look at the API , it’s easy to determine what methods are there, jQuery.something() methods are methods in the jQuery / object variable that are not associated with a set of elements, everything else that only .something() is methods to run with sets elements (in jQuery object instances).

For what you want to do, yes, it is possible, but it does not make much sense, for example:

 $.myFuntion = $.fn.myFunction = function() { alert('hi'); }; 

You should put your methods where it makes sense if you use $.fn.myFunction for a set of elements and this refers to the set of elements inside. If a static method not associated with an element sets it separately or possibly in $.myFunction .

+1
source

You can create a link to a function, but you cannot pass arguments:

 function funky() { alert("Get down with it"); } obj.onclick = funky; 
0
source

Use template literals:

 alert`WOW` 

It is cleaner and shorter, without having to rely on jQuery, make calls, etc.

0
source

All Articles