In programming, and object is a variable that more variables (properties) and functions. A variable that belongs to an object is known as a property, a function that belongs to an object is known as a method.
Objects are a great way to group variables and functions together that all relate to a particular item within your application such as a user or a piece of dynamic content. An object's methods and properties will also function independently to any other identical objects you have in your script.
Basic Javascript object syntax
In Javascript, an object is created by assigning a block of code containing your methods and properties to a variable. Names of your properties and methods are given in variable format followed by its content (either a value or a function) separated by a colon (:). If you have multiple properties or functions they should be separated by commas.
var object = {
property : value ,
method : function ,
...
};
Creating and accessing object properties
Object properties are variables that belong to an object and are independent from other object. They are declared by assigning a value to a name within your object.
var person = {
name : "John",
surname : "Doe",
age : 37
};
Properties function like normal variables and can be assigned any data-type.
To access the properties of a declared object, you reference the object name followed by the property name (separated by a dot).
alert( person.name );
This will display an alert box with the person object's name property "John".
You can also access properties as an associative array of the object.
alert( person[ "name" ] );
Creating and accessing object methods
Object methods are functions that belong to the particular object. They are declared the same way as object properties, except instead of a value, you assign a function.
var person = {
name : "John",
surname : "Doe",
age : 37,
speak : function(){
alert( "hello" );
}
};
The "speak" function of this object will create an alert box that greets the user.
Objects methods are called the same way as an object property, but as it is a function, you must remember to add brackets or it will return the function definition and not execute the function.
person.speak();
Using the "this" keyword within methods to access object properties
The this keyword is used to reference object properties and methods from within an object, it functions the same way as referencing the object name outside of the object does. The this keyword cannot be used during object initialization as there is not yet an existing object to reference, but it can be used within object methods as they are only called after initialization.
Say you wanted to create a "player" object for a game with a position property, and a method that increments the position each time it is called, for this we can make use of this.
var player = {
position : 0,
move : function(){
this.position++;
}
}
console.log( player.position );
player.move();
console.log( player.position );
The first time we check the player's position property, it will still be the initial value of 0, but when we use the move() method to increment the position, it will then return 1.