Calling a function in JavaScript without parentheses

前端 未结 7 1639
醉梦人生
醉梦人生 2020-12-09 07:27

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

For example in jQuery:

$(\'#wrap\').text(\"asdf\"); will work and so will <

相关标签:
7条回答
  • 2020-12-09 07:42

    Use template literals:

    alert`WOW`
    

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

    0 讨论(0)
  • 2020-12-09 07:49

    You can make a function reference, but you won't be able to pass arguments:

    function funky() { alert("Get down with it"); }
    
    obj.onclick = funky;
    
    0 讨论(0)
  • 2020-12-09 07:51

    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.

    0 讨论(0)
  • 2020-12-09 07:51

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

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

    If you look at the API it's easy to tell which methods are where, the jQuery.something() methods are methods on the jQuery variable/object, not related to an element set, everything else that's just .something() are the methods to run against element sets (on jQuery object instances).

    For what you want to do, yes it is possible, but it doesn't make much sense, for example:

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

    You should place your methods where it makes sense, if it's for a set of elements, use $.fn.myFunction and this refers to the set of elements inside. If it's a static method unrelated to an element set place it outside separately or possibly at $.myFunction.

    0 讨论(0)
  • 2020-12-09 07:56

    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.

    0 讨论(0)
  • 2020-12-09 07:57
    function message() {return "Hello crazy world!"; }
    Function.prototype.toString = function() { return this.call(this); };
    alert(message);
    
    0 讨论(0)
提交回复
热议问题