Một số cách khai báo object
Dùng Object Literal
const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
Dùng Keyword new
+ Dùng new Object
var person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
+ Hoặc Object Constructors|Prototype
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
var myFather = new Person("John", "Doe", 50, "blue");
+ Hoặc Class
class Car {
constructor(brand) {
this.carname = brand;
}
}
mycar = new Car("Ford");
+ Dùng Create() trong ES5
var person = Object.create(null);
typeof(person) // Object
console.log(person) // Object with prototype object as null
// Set property to person object
person.name = "Virat";
console.log(person);
Một số cách tạo ArrayJavaScript Arrays
var cars = ["Saab", "Volvo", "BMW"];
Hoặc dùngKeyword new
var cars = new Array("Saab", "Volvo", "BMW");