function properties as arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zac Panepucci

    #1

    function properties as arguments

    Hello There,

    I am using the following constructs to parse named arguments:

    function whatever() {

    if (this.yo) {
    if(this.yo=='ma n')
    alert('wuzup');
    else
    alert('nope');
    }

    if (this.dude) {
    if(this.dude==1 )
    alert('got one dude');
    else
    alert('got no-one dude');
    }

    delete this.dude;
    delete this.yo;
    }

    and I call this function like so:


    whatever(dude=2 , yo='woman');

    How bad is this kind of usage? Can I expect this to work on
    all kinds of javascript implementations ?

    I like it because I can use named arguments that can go
    in any random order when calling the function.

    This currently works on IE 6 and Mozilla Firefox 0.9.

    Any comments?

    Thanks,
    Zac

  • Lasse Reichstein Nielsen

    #2
    Re: function properties as arguments

    Zac Panepucci <zac@slac.stanf ord.edu> writes:
    [color=blue]
    > I am using the following constructs to parse named arguments:
    >
    > function whatever() {
    > if (this.yo) {
    > if(this.yo=='ma n')
    > alert('wuzup');
    > else
    > alert('nope');
    > }[/color]
    ....[color=blue]
    > delete this.dude;[/color]
    ....[color=blue]
    > }[/color]
    [color=blue]
    > and I call this function like so:
    > whatever(dude=2 , yo='woman');
    >
    > How bad is this kind of usage?[/color]

    Bad. It probably "works" but it seems to be based on some
    misconceptions about what is happening.

    When you evaluate the expression
    whatever(dude=2 , yo='woman');
    first the argument expressions are evaluated.

    The first one, dude=2 , is an assignment. It first evaluates its right
    hand side (trivially) giving 2. It then assigns it to the global
    variable "dude" (unless "dude" is also declared as a local variable,
    in that case it breaks). The value of the assignment is the value
    assigne, i.e., 2.

    Then 'woman' is assigned to the global variable "yo".

    Then the function "whatever" is called
    [color=blue]
    > Can I expect this to work on all kinds of javascript
    > implementations ?[/color]

    You can expect it to work equally well on all kinds of javascript
    implementations . Meaning: if, or when, it breaks, it breaks on all
    implementations .

    Try calling whatever from inside this function:
    ---
    function whatnot(dude) {
    var yo = dude + 1;
    whatever(dude = 2, yo = "man"); // fails!
    }
    ---
    It fails because "dude" and "yo" are not global variables in this
    scope. Instead you change the local variables' values, and then
    call "whatever", which tries to read the global variables of the same
    name.

    Also, try passing a named parameter called "document". That will first
    overwrite the global reference to the document object, and then delete
    it. Bad mojo.
    [color=blue]
    > I like it because I can use named arguments that can go
    > in any random order when calling the function.[/color]

    Use an object with object literal notation:
    ---
    function whatever(namedA rgs) {
    if (namedArgs.yo == 1) { ... }
    if (namedArgs.dude == "man") { ... }
    }
    ...
    whatever({dude: "woman", yo:2});
    ---
    This makes a new namespace, the new object, so your names will
    not collide with already existing variables.

    Is it really important that the arugments can go in any order?
    I mean, you are writing the code yourself, you should know the
    order. If it is autogenerated code, then I might see a reason,
    but not a good one. It just smells like someone not wanting
    to commit to the discipline necessary for programming. :)
    [color=blue]
    > This currently works on IE 6 and Mozilla Firefox 0.9.[/color]

    "works" :)
    [color=blue]
    > Any comments?[/color]

    Don't! It's bound to come back and bite you where it hurts when you sit.
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Zac Panepucci

      #3
      Re: function properties as arguments

      > Bad. It probably "works" but it seems to be based on some[color=blue]
      > misconceptions about what is happening.[/color]

      misconception, and a serious one.
      [color=blue]
      > When you evaluate the expression
      > .... [ programming 101 lesson stripped ]...
      >[/color]

      that was very didactic. Thanks.
      [color=blue]
      >
      > Use an object with object literal notation:
      > ---
      > function whatever(namedA rgs) {
      > if (namedArgs.yo == 1) { ... }
      > if (namedArgs.dude == "man") { ... }
      > }
      > ...
      > whatever({dude: "woman", yo:2});
      > ---
      > This makes a new namespace, the new object, so your names will
      > not collide with already existing variables.[/color]

      ok. this is interesting. but then the syntax is not obvious, not for me.
      [color=blue]
      > Is it really important that the arugments can go in any order?[/color]

      I have this function that builds a table of contens by scanning the
      document and matching either a TAG or a CLASS or both.

      I used to call it ehpInsertTOC(ic lass='mysql',it ag='h1');
      or ehpInsertTOC(ic lass='books');
      or ehpInsertTOC(it ag='links');
      [color=blue]
      > I mean, you are writing the code yourself, you should know the
      > order. If it is autogenerated code, then I might see a reason,
      > but not a good one. It just smells like someone not wanting
      > to commit to the discipline necessary for programming. :)[/color]

      I have to work on my discipline.
      [color=blue]
      > Don't! It's bound to come back and bite you where it hurts when you sit.[/color]

      Thanks again Lasse. I fixed my code and bought some ointment.

      Zac

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: function properties as arguments

        Zac Panepucci <zac@slac.stanf ord.edu> writes:

        [color=blue][color=green]
        >> whatever({dude: "woman", yo:2});[/color][/color]
        [color=blue]
        > ok. this is interesting. but then the syntax is not obvious, not for me.[/color]

        Object literal notation is a way to create objects and their
        properties in one go.

        For Arrays, you can either first create the array and then fill in the
        elements:
        var arr = new Array();
        arr[0] = 42;
        arr[1] = 37;
        or, you can use Array literal notation:
        var arr = [42, 37];

        Likewise for Objects, you can either:
        var obj = new Object();
        obj["foo"] = 42;
        obj["bar"] = 37;
        or you can use a literal object:
        var obj = { "foo" : 42, "bar" : 37 };

        The above example called the function 'whatever' with one arguemnt.
        That argument is an object with two properties: "dude" and "yo".
        At the receiving end, the function can use normal object property
        access to get the values.

        [color=blue]
        > I have this function that builds a table of contens by scanning the
        > document and matching either a TAG or a CLASS or both.
        >
        > I used to call it ehpInsertTOC(ic lass='mysql',it ag='h1');
        > or ehpInsertTOC(ic lass='books');
        > or ehpInsertTOC(it ag='links');[/color]

        I would prefer to have a function with two arguments, and to
        pass "null" for the omitted one:
        ehpInsertTOC('m ysql', 'h1');
        ehpInsertTOC('b ooks', null);
        ehpInsertTOC(nu ll, 'links');
        Then the function can check whether the argument is null:
        if (arg1 !== null) { ... }

        Alternatively, if the operations on the arguments are separate, you
        can have three functions, one for each case where there is only
        one argument, and one for the case where there is two:

        function ehpInsertTOCicl ass(iclass) { ... something for class ... }
        function ehpInsertTOCtag (tag) { ... something for tag ... }
        function ehpInsertTOC(ic lass, tag) {
        ehpInsertTOCicl ass(iclass);
        ehpInsertTOCtag (tag);
        }


        Good luck.
        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...