JS анонимные функции - PullRequest
       11

JS анонимные функции

0 голосов
/ 30 декабря 2010

Моя проблема в том, что я хочу вызвать такую ​​функцию, как:

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

и мой js-код:

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

но это не работает (я новичок в js анонимных функциях), где ошибка?

Спасибо за помощь!

1 Ответ

1 голос
/ 30 декабря 2010

Попробуйте

var $ = function(element) {
    // if the function is called without being called as a constructor,
    // then call as a constructor for us.
    // (partially borrowed from /3297940/sozdanie-jquery-podobnogo-obekta )
    if (this.constructor !== $) {
        return new $(element);
    }
    this.doSomething = function(txt) {
        alert(txt + element);
    };
};
...