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,
var foo = {
bar: function () {
alert("Hello, World!");
}
};
// This displays an alert with the text "Hello, World!"
foo.bar();
Also, strings are also objects in Javascript, so they also have functions like that.
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,
var myString = 'this is a string';
alert((new String(myString)).indexOf('is'));
Off Topic:
Happy birthday, Immerse!
1 Like
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