在没有括号的情况下在JavaScript中调用函数 [英] Calling a function in JavaScript without parentheses

查看:169
本文介绍了在没有括号的情况下在JavaScript中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中有没有办法调用没有括号的函数?

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

例如在jQuery中:

For example in jQuery:

$('#wrap')。text(asdf); 将起作用,因此 $。ajax(ajaxOptions);

我正在将一个函数(类)映射到窗口。$ 有一组函数我希望能够使用括号或不使用括号调用。像jQuery一样。

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.

这是一个代码示例:

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

我映射 Test() $

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');


推荐答案

你可以尝试这样的事情

var test = {
    method1 : function(bar) {
        // do stuff here
        alert(bar);
    },
    method2 : function(bar) {
        // do stuff here
        alert(bar);
    }
};

test.method1("foo");
test.method2("fooo");

这篇关于在没有括号的情况下在JavaScript中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆