JavaScript调用变量作为对象和函数,如jQuery

JavaScript调用变量作为对象和函数,如jQuery,javascript,jquery,prototype,Javascript,Jquery,Prototype,我正在尝试为mobile创建一个库,我希望能够像jquery那样调用函数和对象 例如: var test = function(elm) { this.ajax = function(opt) { ... } if ( elm ) { var elms = document.querySelectorAll(elm); } } 我想这样称呼它: test("#id"); test.ajax(opts); Object.prototype.Test =

我正在尝试为mobile创建一个库,我希望能够像jquery那样调用函数和对象

例如:

var test = function(elm) {

    this.ajax = function(opt) { ... }
    if ( elm ) {
       var elms = document.querySelectorAll(elm);
    }
}
我想这样称呼它:

test("#id");
test.ajax(opts);
Object.prototype.Test = function( method ) {
    var method = method || null;
    var elms   = null;

    /* Methods */
    this.ajax = function(opt){
        console.log('You called ajax method with options:');
        console.log(opt);
    }
    /* Logic */
    if (method in this) this[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    else {
        try {
            elms = document.querySelectorAll(method);
        }
        catch(e) {
            console.log(e.message);
        }
    }

}
window.onload = function() {
    Test('ajax', {'url':'testurl.com'});
    Test('#aid');  
}
就像这样:

test("#id");
test.ajax(opts);
Object.prototype.Test = function( method ) {
    var method = method || null;
    var elms   = null;

    /* Methods */
    this.ajax = function(opt){
        console.log('You called ajax method with options:');
        console.log(opt);
    }
    /* Logic */
    if (method in this) this[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    else {
        try {
            elms = document.querySelectorAll(method);
        }
        catch(e) {
            console.log(e.message);
        }
    }

}
window.onload = function() {
    Test('ajax', {'url':'testurl.com'});
    Test('#aid');  
}
乐:
感谢你们的快速回复

在JavaScript中,函数实际上只是一个附加了代码的对象

因此,与普通对象不同:

var test = {};
test.ajax = function() { /* ajax */ };
。。。使用函数:

var test = function() { /* test */ };
test.ajax = function() { /* ajax */ };

在这两种情况下,您都可以访问
test.ajax
。该函数的额外功能是,您可以调用
test

或类似以下内容:

test("#id");
test.ajax(opts);
Object.prototype.Test = function( method ) {
    var method = method || null;
    var elms   = null;

    /* Methods */
    this.ajax = function(opt){
        console.log('You called ajax method with options:');
        console.log(opt);
    }
    /* Logic */
    if (method in this) this[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    else {
        try {
            elms = document.querySelectorAll(method);
        }
        catch(e) {
            console.log(e.message);
        }
    }

}
window.onload = function() {
    Test('ajax', {'url':'testurl.com'});
    Test('#aid');  
}

使用jquery mobile代替您的用例不够详细。您是否试图使
test
函数类似于
jQuery
/
$
对象,以便
test(“#id”)
返回一个对象的实例,该对象上有一个函数“method”
.ajax()
可用?一旦加载DOM并向其附加方法,jQuery就会创建一个新对象(
$
)。然后,它通过返回
this
来使用方法链接。尽管对我来说不是一个很难的问题,+1是为了问一个关于纯Javascript的问题,并试图学习如何工作,而不是使用现有的解决方案,而不知道什么是“幕后黑手”。跟上;)这似乎并没有真正回答这个问题。@ErikE我想这正是OP想要的答案。@ErikE:请你详细说明一下好吗?它回答了如何创建
test
,以便您可以在OP想要的两种方式中使用它。OP的问题已经有了您建议的模式<代码>测试是一个添加了公共方法的函数(尽管在执行时在内部,而不是在初始代码加载时在外部)。你的答案真的有什么不同?您还没有回答重复运行函数将如何返回该函数的不同实例,这些实例都具有相同的函数方法。@ErikE:没有,在OP的情况下,
ajax
不会添加到
test
本身<代码>ajax应该是一个“静态”函数,与实例没有太多关系。OP不需要
new test().ajax
或其他东西,而是
test.ajax
-因此它是函数上的函数,而不是实例上的函数。我不建议设置
Object.prototype.test
来创建全局
test
变量。只需使用
var Test=…
。在许多情况下,使用var Test=new函数(方法)会更好。。。这样他甚至可以使用类似Test.ajax(选项)的调用;但这是唯一的方向:——)