开发者

Argument question javascript

One more question regarding arguments in javascript. I was reading a tutorial about the javascript framework Prototype and came over the following text, I will quote below:

"If you have some DOM methods of your own that you'd like to add to those of Prototype, no problem! Prototype provides a mechanism for this, too. Suppose you have a bunch of functions encapsulated in an object, just pass the object over to Element.addMethods():

var MyUtils = {
    truncate: function(element, length){
        element = $(element);
        return element.update(element.innerHTML.truncate(length));
    },
    updateAndMark: function(element, html){
        return $(element).update(html).addClassName('updated');
    }
}

Element.addMethods(MyUtils);

// now you can:
$('explanation').truncate(100);

The only thing to watch out here is to make sure the first argument of these methods is the element itself. In your methods, you can also return the element in the end to allow for chainability (or, as practiced in the example, any method which itself returns the element)."

Everything seems pretty straightforward except one thing that I seem to have problem understanding. It says that you need to make sure the 开发者_如何转开发first argument of these methods is the element itself. And that is the case in the functions but when you call the function you only provide 1 argument? So for example:

$('explanation').truncate(100);  

There is only 1 argument here that is sent to the function namely 100, but the function signature looks like this:

truncate: function(element, length){

So element comes from where? And is it logic that you must make sure that the first argument is the element itself but you do not provide it when you call the function?


Prototype provides two ways to do things:

  1. Methods on the object itself, for example:

    Element.toggleClassName('selector', 'something');

  2. Via the prototype:

    $('selector').toggleClassName('something');

IIRC, the second method simply calls the first, passing the Element selected to it as well as the original argument. That's what happens in your case as well.


Javascript functions can test their arguments and decide what to do based on what was passed. jQuery is famous for this and it's fairly common in a lot of libraries. It allows one named function to perform many different (but related) operations and often allows shorthand notation that can omit optional or default parameters. You can often pass one, two or three arguments and the code inside the function will examine which arguments are present and, in some cases, the type of the arguments to decide how to treat them.

When functions do this, you have to read their documentation carefully to see which parameters are required and which are optional and what order things can be passed in.

If a function optionally takes three parameters and all three are different types, it's possible for the function to examine the type of each passed parameter and accept them in any order. It can tell which is which by the type. I don't personally think this is good programming style, but it is possible. More likely, it allows you to just leave out some parameters which are not needed and the function will either adapt it's behavior when some things are missing or will set a default for that value.

In your specific example of truncate, I'd have to see a documentation page for that function to explain what it does when no element is passed vs. when one is.

Here's a made up example of a string truncate function that can be used a bunch of different ways:

var str = "This is a long string that I want to be truncated to something shorter";

str.truncate(10);                // truncate to max length of 10
str.truncate("string", "end");   // truncate after the first occurrence of "string"
str.truncate(/\bbe\b/, "begin"); // truncate before the first occurrence of a regular expression
str.truncate("string", 10);      // truncate 10 chars after the first occurrence of "string"

And the outline of this function would look something like this:

string.prototype.truncate = function(lookFor, pos, cnt) {
    if (lookFor == undefined) {
        return(this);   // no parameters passed, nothing to do
    }
    if (typeof lookFor == "Number") {
        // truncate to a certain length
    } else if (typeof lookFor == "String") {
        // look for a plain string
    } else if (lookFor.exec) {
        // look for a regular expression
    } else {
        return(this);  // nothing to do
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜