SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foo.bar() function call

    In a foo.bar() function call, what is the foo relating to and what is the bar relating to?

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    foo is an object of some kind, which you must have declared before using it. bar is the name of a function that is a member of the foo object.

    A simple example,
    Code JavaScript:
    var foo = {
        bar: function () {
                 alert("Hello, World!");
             }
    };
     
     
    // This displays an alert with the text "Hello, World!"
    foo.bar();
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Also, strings are also objects in Javascript, so they also have functions like that.

    Code JavaScript:
    var myString = 'this is a string';
    alert(myString.indexOf('is'));

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    Also, strings are also objects in Javascript, so they also have functions like that.
    Well, in this case you are taking advantage of the fact that JavaScript automatically converts between string values and String objects.

    String literals don't have any functions, but an anonymous String object will be created for you and the function will be invoked via that. So your code is equivalent to,
    Code JavaScript:
    var myString = 'this is a string';
    alert((new String(myString)).indexOf('is'));

    Off Topic:

    Happy birthday, Immerse!
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    String literals don't have any functions, but an anonymous String object will be created for you and the function will be invoked via that. So your code is equivalent to,

    Off Topic:

    Happy birthday, Immerse!
    Never knew that, very interesting! I always thought that strings were always objects in JS. Maybe I'm getting it confused with Ruby?

    Off Topic:

    Thanks


Bookmarks

Posting Permissions

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