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


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


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
Bookmarks