Подтвердить что ты не робот

Функция вызова без круглых скобок

Это меня смущает, я пытаюсь создать идентификатор типа JQuery.

$.ajax
$('object')

идентификатор jQuery $ можно вызвать без его скобок.

Вот код, который я получил:

function initialized_object(){
    this.method = function(){
        console.log('this is a string');
    }
}
var o = function (args){
    if(arguments.length > 0){
        //return N$(arguments[0], arguments[1]);
    }else{
        return new initialized_object();
    }
};
o.prototype.constructor.toString = function(){
    this.call(this);
}
o().method();

Вместо o().method() я хотел бы использовать o.method()

Я посмотрел на источник jquery, пытаясь найти решение этого безрезультатно: http://code.jquery.com/jquery-1.11.3.js


Это то, с чем я работаю:

(если у вас есть идеи)

function N$_no_parameters(){
    this.ajax = function(func){
        func();
    };
};
var N$_np = new N$_no_parameters();

var N$_CURRENT_EVENT_THIS = null;
function N$(selector, within){
    this.co = "hi";
    if (!Array.prototype.indexOf){
        Array.prototype.indexOf = function(elt /*, from*/){
            var len = this.length >>> 0;
            var from = Number(arguments[1]) || 0;
            from = (from < 0) ? Math.ceil(from) : Math.floor(from);
            if (from < 0)
                from += len;
            for (; from < len; from++){
                if (from in this && this[from] === elt)
                    return from;
            }
            return -1;
        };
    }
    var DOM_N$ = function(selector, within, frame){
        this.frame = frame || {
            win: (within != undefined)? within.contentWindow || this.constructor.caller.arguments[0] : this.constructor.caller.arguments[0],
            doc: (within != undefined)? within.contentDocument || this.constructor.caller.arguments[0].document : this.constructor.caller.arguments[0].document,
            this: this.constructor.caller.caller.caller.caller.caller
        };

        if(selector instanceof Object){
            if(selector.defaultView == this.frame.win){//selector is document
                this.selector = selector;
                this.nodes = [this.selector];
            }else if(selector.document == document){//selector is window
                this.selector = selector;
                this.nodes = [this.selector];
            }else if(selector instanceof this.frame.this){// selector is this
                this.selector = N$_CURRENT_EVENT_THIS.selector;
                this.nodes = N$_CURRENT_EVENT_THIS.nodes;//
            }else if(selector instanceof Element){//selector is DOM
                this.selector = selector;
                this.nodes = [this.selector];
            }else{
                this.selector = selector;
                this.nodes = new Array();
                for(var key in selector){
                    var dom_n$_ = N$(selector[key]).nodes;
                    for(var key_ in dom_n$_){
                        if(this.nodes.indexOf(dom_n$_[key_]) == -1){
                            this.nodes.push(dom_n$_[key_]);
                        }
                    }
                }
            }
        }else if(typeof selector == "string"){
            this.selector = selector;
            this.nodes = $prepare(this.selector, this.frame.doc);
        }
        this.event = function(event_, func){
            var that = this;
            actionair(function(node){
                var events = node.events || {};
                if(node.addEventListener){
                    if((event_) in events){
                        node.removeEventListener(event_, events[event_], true);
                        var tmp___ = events[event_];
                        var tmp__ = function(){
                            this.bar = "hello";
                            N$_CURRENT_EVENT_THIS = that;
                            tmp___(node, event_);
                            new func(node, event_);
                            N$_CURRENT_EVENT_THIS = null;
                        };
                        node.addEventListener(event_, tmp__, true);
                        events[event_] = tmp__;
                    }else{
                        var tmp__ = function(){
                            N$_CURRENT_EVENT_THIS = that;
                            new func(node, event_);
                            N$_CURRENT_EVENT_THIS = null;
                        };
                        node.addEventListener(event_, tmp__, true);
                        events[event_] = tmp__;
                    }
                }else if(node.attachEvent){
                    var ie_event = 'on' + event_;
                    if(event_ in events){
                        node.attachEvent(ie_event, function(){
                            N$_CURRENT_EVENT_THIS = that;
                            new func(node, event_);
                            events[event_](node, event_);
                            N$_CURRENT_EVENT_THIS = null;
                        });
                    }else{
                        node.attachEvent(ie_event, function(){
                            N$_CURRENT_EVENT_THIS = that;
                            new func(node, event_);
                            N$_CURRENT_EVENT_THIS = null;
                        });
                    }
                    events[event_] = func;
                }
                node.events = events;

            }, this);
        }

        this.removeEvent = function(event_){
            actionair(function(node, that){
                var events = node.events || {};
                if(node.removeEventListener){
                    if((event_) in events){
                        node.removeEventListener(event_, events[event_], true);
                        events[event_] = null;
                    }
                }else if(node.detachEvent){
                    var ie_event = 'on' + event_;
                    if((event_) in events){
                        node.detachEvent(ie_event, events[event_]);
                        delete events[event_];
                    }
                }
            }, this);
        }
        this.eachNode = function(func){
            actionair(function(node, that){
                N$_CURRENT_EVENT_THIS = N$(node);
                new func(node);
            }, this);
        }
        this.css = function(attr, value){
            N$_CURRENT_EVENT_THIS = this;
            var attribute = "";
            if(attr.indexOf('-') !== -1){
                var split_attr = attr.split('-');
                for (var i = 0; i < split_attr.length; i++) {
                    if(i != 0)
                        attribute += split_attr[i].charAt(0).toUpperCase() + split_attr[i].slice(1);
                    else
                        attribute += split_attr[i].charAt(0).toLowerCase() + split_attr[i].slice(1);
                };
            }else{
                attribute = attr;
            }

            var properties = new Array();


            actionair(function(node, that){
                if(typeof value != 'undefined'){
                    node.style[attribute] = value;
                }
                if (!that.frame.win.getComputedStyle) {//IE
                    that.frame.win.getComputedStyle = function(el, pseudo) {
                        that.el = el;
                        that.getPropertyValue = function(prop) {
                            var re = /(\-([a-z]){1})/g;
                            if (prop == 'float') prop = 'styleFloat';
                            if (re.test(prop)) {
                                prop = prop.replace(re, function () {
                                    return arguments[2].toUpperCase();
                                });
                            }
                            return el.currentStyle[prop] ? el.currentStyle[prop] : null;
                        }
                        return that;
                    }
                }
                properties.push(that.frame.win.getComputedStyle(node, null).getPropertyValue(attr));

            }, this);
            return properties;
        };

        this.text = function(str){
            actionair(function(node, that){
                node.innerHTML = '';
                node.appendChild(that.frame.doc.createTextNode(str));
            }, this);
        };
        this.appendNode = function(tagname, innerHTML){
            actionair(function(node, that){
                var new_node = that.frame.doc.createElement(tagname);
                new_node.innerHTML = innerHTML;
                node.appendChild(new_node);

            }, this);
        };
        this.innerHTML = function(innerHTML){
            actionair(function(node, that){
                node.innerHTML = innerHTML;
            }, this);
        };
        this.removeNode = function(){
            actionair(function(node, that){
                node.parentNode.removeChild(node);
            }, this);
        };
        this.animate = function(func, from, to, speed){

            var that = this;
            actionair(function(node, that){
                (function animate(func, from, to, speed, node){
                    if(from >= to){
                        N$_CURRENT_EVENT_THIS = that;
                        new func(node, to);
                        N$_CURRENT_EVENT_THIS = null;
                    }else{
                        N$_CURRENT_EVENT_THIS = that;
                        new func(node, from);
                        N$_CURRENT_EVENT_THIS = null;
                        setTimeout(
                            function(){
                                animate(func, from+1, to, speed, node);
                            }, speed
                        );
                    }
                })(func, from, to, speed, node);
            }, this);
        }

        function actionair(func, that){
            for (var i = 0; i < that.nodes.length; i++) {
                (function(i_){
                    N$_CURRENT_EVENT_THIS = that;
                    new func(that.nodes[i_], that);
                    N$_CURRENT_EVENT_THIS = null;
                })(i);
            }
        }
        function $prepare(str, doc){
            str = str.replace(/(\s+>\s+)/g,'>');
            str = str.replace(/(\s+)/g,' ');
            var str_ = str;
            var querys = str.split(/[\s\>]+/);
            var querys_des = Array();

            var ascender = new Array();
            for (var i = 0; i < str_.length; i++) {
                if(str_[i] == ">" || str_[i] == " "){
                    var tmp_ = (str_[i] == ">")? 'next_child' : 'ascended';
                    ascender.push( tmp_);
                }
            };
            var recognizes = new Array();
            for (var i = 0; i < querys.length; i++) {
                var asc_child = null;
                asc_child = ascender[i-1];
                var tmp_ = {
                    "selector": querys[i],
                    "i":i
                };
                recognizes[i] = recognize(querys[i], doc);
                if(i != 0){
                    tmp_["asc_child"] = asc_child;
                }else{
                    tmp_["base_selector"] = true;
                }
                querys_des.push(tmp_);
            };
            return $select(querys_des, recognizes, doc);
        }
        function $select(querys_des, recognizes, parent_, doc){
            var parents = parent_ || null;
            for (var i = 0; i < querys_des.length; i++) {
                if('base_selector' in querys_des[i]){
                    parents = recognizes[querys_des[i]['i']];
                }else if('asc_child' in querys_des[i]){
                    var cur_children = recognizes[querys_des[i]['i']];
                    if(querys_des[i]['asc_child'] == 'next_child'){
                        var compatible = compatible_children(parents, cur_children, querys_des[i]['asc_child'], doc);
                        parents = compatible;
                    }else if(querys_des[i]['asc_child'] == 'ascended'){
                        var compatible = compatible_children(parents, cur_children, querys_des[i]['asc_child'], doc);
                        parents = compatible;
                    }
                }
            };

            return parents;
        }

        function compatible_children(parents, children, type, doc){
            var ret = new Array();
            for (var a = 0; a < parents.length; a++) {
                for (var b = 0; b < children.length; b++) {
                    if(type == 'next_child'){
                        if(parents[a] == children[b].parentNode){
                            if(ret.indexOf(children[b]) == -1)
                                ret.push(children[b]);
                        }
                    }else if(type == 'ascended'){
                        if(isin(parents[a], children[b], doc)){
                            if(ret.indexOf(children[b]) == -1)
                                ret.push(children[b]);
                        }
                    }
                }
            }
            return ret;
        }

        function isin(parent, child, doc){
            var child_ = child;
            var ret = new Array();
            while((child_ = child_.parentNode) && child_ != doc.body){
                if(parent == child_){
                    return true;
                }
            }
            return false;
        }

        function recognize(str, doc){
            var identifier = new Array();

            var id_ = false;
            var class_ = false;
            var dom_ = false;
            if(str.indexOf("#") >= 0){
                id_ = true;
                var tmp = str.split("#")[1];
                if(str.indexOf(".") >= 0){
                    identifier['ID'] = tmp.split(".")[0];
                }else{
                    identifier['ID'] = tmp;
                }
            }

            if(str.indexOf(".") >= 0){
                class_ = true;
                var tmp = str.split(".")[1];
                if(str.indexOf("#") >= 0){
                    identifier['CLASS'] = tmp.split("#")[0];
                }else{
                    identifier['CLASS'] = tmp;
                }
            }


            if(id_ && class_){
                if(str.indexOf("#") < str.indexOf(".")){
                    var tmp = str.split("#")[0];
                    if(tmp.length > 0){
                        dom_ = true;
                        identifier['DOM'] = tmp;
                    }
                }else{
                    var tmp = str.split(".")[0];
                    if(tmp.length > 0){
                        dom_ = true;
                        identifier['DOM'] = tmp;
                    }
                }
            }else if(id_){
                var tmp = str.split("#")[0];
                if(tmp.length > 0){
                    dom_ = true;
                    identifier['DOM'] = tmp;
                }
            }else if(class_){
                var tmp = str.split(".")[0];
                if(tmp.length > 0){
                    dom_ = true;
                    identifier['DOM'] = tmp;
                }
            }else{
                if(str.length > 0){
                    dom_ = true;
                    identifier['DOM'] = str;
                }
            }


            var x;
            if(class_){
                if(typeof doc.getElementsByClassName !== 'function') {//Old browsers
                    x = doc.body.getElementsByTagName("*");
                }else{
                    x = doc.getElementsByClassName(identifier['CLASS']);
                }

            }else if(dom_){
                x = doc.getElementsByTagName(identifier['DOM']);
            }else if(id_){
                x = doc.body.getElementsByTagName("*");
                for (var i = 0; i < x.length; i++) {
                    if(x[i].getAttribute("id") != identifier['ID']){
                        delete x[i];
                    }
                };
            }

            var elements = new Array();


            for (var i = 0; i < x.length; i++) {
                if(id_ && class_){
                    if(x[i].getAttribute("id") == identifier["ID"] && x[i].getAttribute("class") == identifier["CLASS"]){
                        if(dom_){
                            if(x[i].tagName.toLowerCase() == identifier['DOM'].toLowerCase()){
                                elements.push(x[i]);
                            }
                        }else{
                            elements.push(x[i]);
                        }
                    }
                }else if(id_){
                    if(x[i].getAttribute("id") == identifier["ID"]){
                        if(dom_){
                            if(x[i].tagName.toLowerCase() == identifier['DOM'].toLowerCase()){
                                elements.push(x[i]);
                            }
                        }else{
                            elements.push(x[i]);
                        }
                    }
                }else if(class_){
                    if(x[i].getAttribute("class") == identifier["CLASS"]){
                        if(dom_){
                            if(x[i].tagName.toLowerCase() == identifier['DOM'].toLowerCase()){
                                elements.push(x[i]);
                            }
                        }else{
                            elements.push(x[i]);
                        }
                    }
                }else{
                    if(dom_){
                        if(x[i].tagName.toLowerCase() == identifier['DOM'].toLowerCase()){
                            elements.push(x[i]);
                        }
                    }else{
                        elements.push(x[i]);
                    }
                }

            };

            return elements;
        }
    };

    var selectors = new Array();
    console.log('arguments' + arguments.length);
    if(arguments.length > 0){
        return new (function(selector, within){
            if(typeof within == typeof {}){
                if(within.nodes != undefined){
                    var ret = new Array();
                    for (var i = within.nodes.length - 1; i >= 0; i--) {
                        ret.push(do_node_select(selector, within.nodes[i]));
                    };
                    return ret;
                }else if(
                    typeof Node === "object" ? within instanceof Node : 
                    within && typeof within === "object" && typeof within.nodeType === "number" && typeof within.nodeName==="string"
                ){
                    return do_node_select(selector, within);
                }
            }
            return do_node_select(selector, undefined);

            function do_node_select(selector, node){


                    var N$_new = new ( function(win, doc){
                        return new DOM_N$(selector, node || undefined);
                    })(window);

                    var N$_ = null;
                    if(selectors.length > 0){
                        for (var i = selectors.length - 1; i >= 0; i--) {
                            if(selectors[i].selector == selector){
                                var not_in = new Array();
                                for (var b = N$_new.nodes.length - 1; b >= 0; b--) {
                                    if(selectors[i].nodes.indexOf(N$_new.nodes[b]) == -1){
                                        not_in.push(N$_new.nodes[b]);
                                    }
                                };
                                for (var a = not_in.length - 1; a >= 0; a--) {
                                    if(selectors[i].nodes.indexOf(not_in[a]) == -1){
                                        selectors[i].nodes.push(not_in[a]);
                                    }
                                };
                                N$_ = selectors[i];
                                break;
                            }else{
                                N$_ = N$_new;
                            }
                        };
                    }else{
                        N$_ = N$_new;
                        if(N$_.nodes.length > 0){
                            selectors.push(N$_);
                        }
                    }
                    return N$_;

            }
        })(selector, within || undefined);
    }else{
        return N$_np;
    }

};


N$(window).event('load', function(){

    N$.ajax(function(){ // this will not work but using N$().ajax will
        console.log('aaa');
    });
});

Это библиотека, которая похожа на JQuery, она выбирает узлы и обрабатывает события и многое другое. причина, по которой я хотел бы назвать мою функцию ajax без скобок, для ясности.

4b9b3361

Ответ 1

Идентификатор jquery $можно вызывать без его скобок.

Нет. Функция не вызывается.

Функции - это объекты. Объекты могут иметь свойства. Это просто доступ к свойству объекта функции.

function foo() {
  return 1;
}

foo.bar = 2;

alert(foo.bar);
alert(foo());

Ответ 2

$- функция. Но функции в JavaScript также являются объектами, которые в свою очередь могут иметь функции.

Возможно, этот шаблон ближе к тому, что вы ищете:

var o = {
    method: function() {
    }
};
o.method();

Ответ 3

Каждый раз, когда вы вызываете o(), вы получаете новый экземпляр, который может вести себя по-другому в зависимости от того, как вы его называете.

Если вам нужно просто позвонить o.method, как он узнает, какую ссылку использовать? Чтобы быть похожим на jQuery, вы бы сделали

o = o() // or o('something')

который создаст глобальный синглтон o, который вы могли бы затем вызвать o.method.

Ответ 4

Ответ Quentim полностью верен. Ты просто обладаешь имуществом, неважно. Я просто хотел добавить, что есть способ "вызвать функцию без скобок", если вы используете getters:

window.__defineGetter__("$", function() { 
    //This gets executed when you access $
    console.log('oh hai'); 
    return { foo: 'bar' } 
});

Не то, чтобы это было сделано вообще, но это способ достижения того, что в заголовке вопроса