Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以参数作为对象的Javascript函数_Javascript - Fatal编程技术网

以参数作为对象的Javascript函数

以参数作为对象的Javascript函数,javascript,Javascript,我需要知道如何使用jquery的$('x').y来创建我的站点。有人能帮忙吗? $('x').y myfunction('something').method 附言:我需要像emyFunction(“某物”)这样的东西。方法 myfunction('something').method 对不起,如果太困惑了,我的英语说得不太好…要制作这样的作品: myfunction('something').method myfunction('something').method() myfunction

我需要知道如何使用jquery的$('x').y来创建我的站点。有人能帮忙吗?
$('x').y myfunction('something').method 附言:我需要像emyFunction(“某物”)这样的东西。方法
myfunction('something').method
对不起,如果太困惑了,我的英语说得不太好…

要制作这样的作品:

myfunction('something').method
myfunction('something').method()
myfunction
只需要返回一个具有
.method
属性的对象。jQuery就是这样做的

下面是一个简单的例子:

function myfunction() {
    // do whatever your function wants here

    // then return an object with the .method property
    return {
        method: function() {
            console.log("Hi");
            return this;
        }
    }
}

myfunction().method();

而且,如果从每个方法返回
this
,则可以链接多个方法(就像jQuery那样):

function myfunction() {
    return {
        cntr: 0,
        method1: function() {
            console.log("Hi");
            ++this.cntr;
            return this;
        },
        method2: function() {
            console.log("Goodbye");
            ++this.cntr;
            return this;
        }
    }
}

myfunction().method1().method2();
在这里查看:
使用属性创建对象。按字符串查找这些属性

_("name").method();
_("name2").method();



function _ (property) {
    var obj = {
        name : {
            method : function () { }
        },
        name2 : {
            method : function () { }
        }
    };

    return obj[property];
}
这是一个非常非常基本的版本,非常未优化,而且很难扩展和维护。
jQuery通过查找对象并缓存它们来工作。
在这里,您没有查找DOM对象;这只是函数内部的一个对象,每次调用函数时都会重新生成该对象

…你为什么特别需要这个?
为什么你不能使用不同的系统

制作这样的东西非常简单,不需要很多JS知识;维护它很难。
myfunction('something').method
制作一些易于维护的东西(如jQuery或Angular)是可以做到的,但这需要做大量的工作,在尝试之前,你必须真正理解JS。

版本一,要想了解这个想法:

function multiplyBy(n) {
    return function(arg) {
        return n * arg;
    };
}

var multiplyByFive = multiplyBy(5);
console.log(multiplyByFive(10)); // Outputs 50
换句话说,函数只是值,通过在名称(引用)后粘贴
()
来调用

版本2,基本上也是一个模块模式–返回一个成员为函数的对象:

function operandIs(n) {
    return {
        multiply: function(arg) { return arg * n;  },
        divide:   function(arg) { return arg / 10; }
    }
}

var by10 = operandIs(10);
console.log(by10.multiply(10)); // Outputs 100
console.log(by10.divide(10));   // Outputs 1

进一步考虑:您还可以使用原型方法等返回新函数。

您只需从函数返回对象即可

function thats() {
    return { 
        how: function () {
            return {
                it: function () {
                    return {
                        works: function () {
                            console.log("that's how it works!");
                        }
                    };
                }
            };
        } 
    };
}

thats().how().it().works();

你想要实现什么?请澄清你想要实现什么@森格索夫特汉克斯,@jfriend00。正是我想要的。抱歉,如果问题太简单,我是JS面向对象编程的新手。