They are objects, but they are not object literals.
A function can be used to create an instance of an object. For example:
function Person(name, age) {
this.name = name;
this.age = age;
this.sayHello = function() {
console.log('Hi, my name is ' + this.name);
};
}
var bob = new Person('Bob', 24);
The bob
variable is an instance of the Person
function.
You can access the properties from bob
like this:
console.log( bob.name ) // "Bob"
You could also define it literally, like this:
var bob = {
name: 'Bob',
age: 24
};
The function
syntax is used to create functions. They are templates that can be used over and over again. The object literal syntax is used when you only want one instance, or also for data, without any behavior attached to it.