<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Object-oriented JavaScript example</title>
</head>
<body>
<p>
This example requires you to enter commands in your browser's JavaScript
console (see
<a
href="https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Tools_and_setup/What_are_browser_developer_tools"
>What are browser developer tools</a
>
for more information).
</p>
<script></script>
</body>
</html>JavaScript object basics / notebook
JavaScript object basics
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const person = {};const person = {
name: ["Bob", "Smith"],
age: 32,
bio: function () {
console.log(`${this.name[0]} ${this.name[1]} is ${this.age} years old.`);
},
introduceSelf: function () {
console.log(`Hi! I'm ${this.name[0]}.`);
},
};person.name;
person.name[0];
person.age;
person.bio();
// "Bob Smith is 32 years old."
person.introduceSelf();
// "Hi! I'm Bob."const objectName = {
member1Name: member1Value,
member2Name: member2Value,
member3Name: member3Value,
};const person = {
name: ["Bob", "Smith"],
age: 32,
bio() {
console.log(`${this.name[0]} ${this.name[1]} is ${this.age} years old.`);
},
introduceSelf() {
console.log(`Hi! I'm ${this.name[0]}.`);
},
};person.age;
person.bio();const person = {
name: ["Bob", "Smith"],
};const person = {
name: {
first: "Bob",
last: "Smith",
},
// …
};person.name.first;
person.name.last;name[0];
name[1];name.first;
name.last;person.age;
person.name.first;person["age"];
person["name"]["first"];const person = {
name: ["Bob", "Smith"],
age: 32,
};
function logProperty(propertyName) {
console.log(person[propertyName]);
}
logProperty("name");
// ["Bob", "Smith"]
logProperty("age");
// 32person.age = 45;
person["name"]["last"] = "Cratchit";person.age;
person["name"]["last"];person["eyes"] = "hazel";
person.farewell = function () {
console.log("Bye everybody!");
};person["eyes"];
person.farewell();
// "Bye everybody!"const myDataName = nameInput.value;
const myDataValue = nameValue.value;person[myDataName] = myDataValue;const myDataName = "height";
const myDataValue = "1.75m";
person[myDataName] = myDataValue;person.height;const person = {
// …
introduceSelf() {
console.log(`Hi! I'm ${this.name[0]}.`);
},
};const person1 = {
name: "Chris",
introduceSelf() {
console.log(`Hi! I'm ${this.name}.`);
},
};
const person2 = {
name: "Deepti",
introduceSelf() {
console.log(`Hi! I'm ${this.name}.`);
},
};function createPerson(name) {
const obj = {};
obj.name = name;
obj.introduceSelf = function () {
console.log(`Hi! I'm ${this.name}.`);
};
return obj;
}const salva = createPerson("Salva");
salva.introduceSelf();
// "Hi! I'm Salva."
const frankie = createPerson("Frankie");
frankie.introduceSelf();
// "Hi! I'm Frankie."function Person(name) {
this.name = name;
this.introduceSelf = function () {
console.log(`Hi! I'm ${this.name}.`);
};
}const salva = new Person("Salva");
salva.introduceSelf();
// "Hi! I'm Salva."
const frankie = new Person("Frankie");
frankie.introduceSelf();
// "Hi! I'm Frankie."myString.split(",");const myDiv = document.createElement("div");
const myVideo = document.querySelector("video");const myNotification = new Notification("Hello!");