xxxxxxxxxx
let car = {
engineNumber: 1234
brand: 'BMW',
break: function (){}
}
xxxxxxxxxx
var student = { // object name
firstName:"Jane", // list of properties and values
lastName:"Doe",
age:18,
height:170,
fullName : function() { // object function
return this.firstName + " " + this.lastName;
}
};
xxxxxxxxxx
class ObjectLayout {
constructor() {
this.firstName = "Larry"; //property
}
sayHi() { // Method
return `Hello from ${this.firstName}`;
}
}
const object = new ObjectLayout();
// object.firstName is Larry;
// object.sayHi() gives "Hello from Larry"
xxxxxxxxxx
// To make an object literal:
const dog = {
name: "Rusty",
breed: "unknown",
isAlive: false,
age: 7
}
// All keys will be turned into strings!
// To retrieve a value:
dog.age; //7
dog["age"]; //7
//updating values
dog.breed = "mutt";
dog["age"] = 8;
xxxxxxxxxx
const objectName = {
member1Name: member1Value,
member2Name: member2Value,
member3Name: member3Value
};
xxxxxxxxxx
const person = {
name: 'Anthony',
age: 32,
city: 'Los Angeles',
occupation: 'Software Developer',
skills: ['React','JavaScript','HTML','CSS']
}
//Use Template Literal to also log a message to the console
const message = `Hi, I'm ${person.name}. I am ${person.age} years old. I live in ${person.city}. I am a ${person.occupation}.`;
console.log(message);
xxxxxxxxxx
var myObject = {}; // Empty object
// Object with properties
var person = {
name: "John Doe",
age: 30,
occupation: "Developer"
};
console.log(person.name); // Output: "John Doe"
console.log(person.age); // Output: 30
console.log(person.occupation); // Output: "Developer"