简体   繁体   中英

Javascript function with arguments as object

I need to know how to do something like jquery's

$('x').y
to make my site. Can anyone help?
PS: I need something like
 myfunction('something').method  

Sorry if too confused, I don't speak english very well...

To make something like this work:

myfunction('something').method()

myfunction just needs to return an object that has .method as a property. That is exactly how jQuery does it.

Here's a simple example:

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();

And, you can chain multiple methods (like jQuery does) if you return this from each method:

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();
$.fn.greenify = function() {
    this.css( "color", "green" );
};

$( "a" ).greenify(); // Makes all the links green.

check out here: http://learn.jquery.com/plugins/basic-plugin-creation/

Make an object with properties. Look those properties up by string.

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



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

    return obj[property];
}

That's a very, very basic version, which is very unoptimized, and also very hard to grow and maintain.
jQuery works by looking the objects up, and caching them.
Here, you aren't looking up DOM objects; this is just an object inside of the function, which gets rebuilt every time the function is called.

...why do you need this, specifically?
Why can't you use a different system?

Making something like this is very easy, and doesn't take a lot of JS knowledge; maintaining it is hard.
Making something that's easy to maintain (like jQuery or Angular) can be done, but it's a lot of work, and you really have to understand JS, before you try.

Version one, to get the idea:

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

var multiplyByFive = multiplyBy(5);
console.log(multiplyByFive(10)); // Outputs 50

In other words, functions are just values, called by sticking () after their names (references).

Version two, which is also basically a module pattern–return an object whose members are functions:

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

Further thought: you can also return new functions with prototype methods etc.

You just have to return objects from functions.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM