开发者

JS anonymous functions

my problem is that I want to call a function like:

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

and my js-code is:

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

but that does not wor开发者_运维百科k (I'm new to js anonymous functions), where's the error?

Thanks for help!


Try

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);
    };
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜