Object
Đối tượng là kiểu dữ liệu không nguyên thủy trong JavaScript. Nó giống như bất kỳ biến nào khác, sự khác biệt duy nhất là một đối tượng chứa nhiều giá trị về các thuộc tính và phương thức. Các thuộc tính có thể chứa các giá trị của các kiểu dữ liệu nguyên thủy và các phương thức là các hàm.
Let's learn how to create an object in JavaScript.
In JavaScript, an object can be created in two ways:
- Object literal
- Object constructor
Object Literal
The object literal is a simple way of creating an object using { } brackets. You can include key-value pair in { }, where key would be property or method name and value will be value of property of any data type or a function. Use comma (,) to separate multiple key-value pairs.
var <object-name> = { key1: value1, key2: value2,... keyN: valueN};
The following example creates an object using object literal syntax.
var emptyObject = {}; // object with no properties or methods
var person = { firstName: "John" }; // object with single property
// object with single method
var message = { showMessage: function (val) { alert(val); } };
// object with properties & method
var person = { firstName: "James", lastName: "Bond", age: 15, getFullName: function () { return this.firstName + ' ' + this.lastName } };
You must specify key-value pair in object for properties or methods. Only property or method name without value is not valid. The following syntax is invalid.
var person = { firstName };
var person = { firstName: };
Access JavaScript Object Properties & Methods
You can get or set values of an object's properties using dot notation or bracket. However, you can call an object's method only using dot notation.
var person = { firstName: "James", lastName: "Bond", age: 25, getFullName: function () { return this.firstName + ' ' + this.lastName } };
person.firstName; // returns James
person.lastName; // returns Bond
person["firstName"];// returns James
person["lastName"];// returns Bond
person.getFullName();
Object Constructor
The second way to create an object is with Object Constructor using new keyword. You can attach properties and methods using dot notation. Optionally, you can also create properties using [ ] brackets and specifying property name as string.
var person = new Object();
// Attach properties and methods to person object
person.firstName = "James";
person["lastName"] = "Bond";
person.age = 25;
person.getFullName = function () { return this.firstName + ' ' + this.lastName; };
// access properties & methods
person.firstName; // James
person.lastName; // Bond
person.getFullName(); // James Bond
Undefined Property or Method
JavaScript will return 'undefined' if you try to access properties or call methods that do not exist.
If you are not sure whether an object has a particular property or not, then use hasOwnProperty() method before accessing properties.
var person = new Object();
person.firstName; // returns undefined
if(person.hasOwnProperty("firstName")){ person.firstName; }
Access Object Keys.
Use for..in loop to get the list of all properties and methods of an object.
var person = new Object();
person.firstName = "James";
person["lastName"] = "Bond";
person.age = 25;
person.getFullName = function () { return this.firstName + ' ' + this.lastName; };
for(var key in person){ alert(key); };
Pass by Reference
Object in JavaScript passes by reference from one function to another.
function changeFirstName(per)
{ per.firstName = "Steve";
}
var person = { firstName : "Bill" };
changeFirstName(person)
person.firstName; // returns Steve
If, two objects point to the same object then the change made in one object will reflect in another object.
var person = { firstName : "John" };
var anotherPerson = person;
anotherPerson.firstName = "Bill";
person.firstName; //returns Bill
You can assign another object as a property of an object.
var person = { firstName: "James", lastName: "Bond", age: 25, address: { id: 1, country:"UK" }
};
person.address.country; // returns "UK"
- JavaScript object is a standalone entity that holds multiple values in terms of properties and methods.
- Object property stores a literal value and method represents function.
- An object can be created using object literal or object constructor syntax.
- Object literal:
-
var person = { firstName: "James", lastName: "Bond", age: 25, getFullName: function () { return this.firstName + ' ' + this.lastName } };
- Object constructor:
-
var person = new Object(); person.firstName = "James"; person["lastName"] = "Bond"; person.age = 25; person.getFullName = function () { return this.firstName + ' ' + this.lastName; };
- Object properties and methods can be accessed using dot notation or [ ] bracket.
- An object is passed by reference from one function to another.
- An object can include another object as a property.