Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts

    JS variable.doSomething()

    I want to create a JS method (I think that's what they're called) where you write variable.doSomething(argument) and the doSomething function can access the variable. But the doSomething method is not tied to a specific object. You can have anything.doSomething() and it will still work. How would that be done?

    Thanks,
    Jazzo
    Last edited by Jazzo; 12-02-2009 at 07:17 PM.
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Jazzo View Post
    I want to create a JS method (I think that's what they're called) where you write variable.doSomething(argument) and the doSomething function can access the variable. But the doSomething method is not tied to a specific object. You can have anything.doSomething() and it will still work. How would that be done?

    Thanks,
    Jazzo
    A method of which object? In order to do what? can you detail a little bit, please?

    I guess you need a constructor.
    http://www.javascriptkit.com/javatutors/oopjs2.shtml
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Well, it's for a JS library so I don't want to have someone else do it for me, but basically this. I am making an inArray function. I want you to be able to do value.inArray(array) and it will return true if the value is in the array. I already coded the function, I just don't know how to set it up that way.
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Code:
    Array.prototype.inArray = function( val )
        {
            for ( var i = 0; i < this.length; ++i )
            {
                if ( this[i] == val ) return true;
            }
            return false;
        }
    
    var x = [ 77, 88, 99 ];
    alert( x.inArray(101) );
    var y = new Array( "aardvark", "bear", "camel", "dog" );
    alert( y.inArray("bear") );
    I'm sure you can see how to extend that to allow an array as the argument to the method.

    You *CAN* attach the method to the Object prototype, and then test "this" to see if it's an array, but it seems kind of pointless, to me.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Jazzo (12-02-2009)

  • #5
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Thank you (once again) old pedant!

    Jazzo
    ~Julian
    14 y/o web developer, drummer, and Bridge player


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •