0

我的问题是我想调用如下函数:

$('div').doSomething('xyz');

我的js代码是:

var $ = function(element) {
    var doSomething = function(xyz, xzy, zxy) {
        alert(xyz + element);
    };
};

但这不起作用(我是 js 匿名函数的新手),错误在哪里?

感谢帮助!

4

1 回答 1

1

尝试

var $ = function(element) {
    // if the function is called without being called as a constructor,
    // then call as a constructor for us.
    // (partially borrowed from http://stackoverflow.com/questions/4556110/creating-a-jquery-like-object )
    if (this.constructor !== $) {
        return new $(element);
    }
    this.doSomething = function(txt) {
        alert(txt + element);
    };
};
于 2010-12-30T16:38:00.863 回答