// 创建一个简单的 JavaScript 对象
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
occupation: "Developer",
// 对象中可以包含方法
getFullName: function() {
return this.firstName + " " + this.lastName;
}
};
// 访问对象的属性
console.log(person.firstName); // 输出: John
console.log(person.age); // 输出: 30
// 调用对象的方法
console.log(person.getFullName()); // 输出: John Doe
// 添加新属性
person.email = "john.doe@example.com";
console.log(person.email); // 输出: john.doe@example.com
// 修改现有属性
person.age = 31;
console.log(person.age); // 输出: 31
{} 创建了一个名为 person 的对象,其中包含了多个属性(如 firstName, lastName, age, occupation)和一个方法 getFullName。.)访问对象的属性,例如 person.firstName。.)调用对象的方法,例如 person.getFullName()。person.email。person.age 修改为 31。上一篇:js window对象
下一篇:js 对象类型
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站