在 JavaScript 中创建对象列表
本文将演示在 JavaScript 中创建对象列表及其属性和方法。
JavaScript 是一种面向对象的编程语言,在任何面向对象的编程语言中,一切都被视为对象。
对象是具有状态和行为的现实世界实体 - 例如,汽车。汽车具有状态(颜色)和行为(速度、加速度等)。
通常,一个对象伴随着其他编程语言中的一个类,而类定义了一个对象的蓝图。但是 JavaScript 不需要类来创建对象,因为它纯粹基于模板。
语法:
const object = {
// object's different member's names & values
name1: value1,
name2: value2,
name3: value3
};
让我们看一个简单的 JavaScript 演示。
const employee = {
id: 541,
company: ['Delft', 'Stack'],
details: function() {
document.write('I work for ${this.name[0]} ${this.name[1]} with employee id ${this.id}');
// I work for DelftStack with employee id 541
}
};
上述员工对象的前两个成员是数据项,我们通常称它们为对象的属性/状态。第三项是允许对象对数据做某事的函数,我们通常称它为对象的方法/行为。
简单来说,对象就是属性的集合。JavaScript 对象中的数据项或值称为其属性。
除了只读属性,我们还可以添加、更新和删除属性。
在下面的代码中,我们解释了访问对象属性的不同方法。
var student = {
rollNo: "BSE541",
name: "Joe Burns",
age: 23,
degree: "Computer Science",
};
student.age; // accessing object using object.property
student[age]; // accessing object using object[property]
x = "age";
student[x]; // using variable to access object's property
简单来说,JavaScript 对象可以执行的操作称为方法。
var student = {
rollNo: "BSE541",
name: "Joe Burns",
age: 23,
degree: "Computer Science",
};
document.getElementById("demo").innerHTML = "Hi " + student.name;
// print "Hi Joe Burns"
下面是演示 JavaScript 对象属性和方法的完整 HTML 示例。
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects Example</h2>
<p id="object"> </p>
<script>
const company = {
name:"DelftStack",
id:571,
location:"USA"
};
document.getElementById("object").innerHTML = company.name + " is a technology company registered in the " + company.location + " with id: " + company.id;
</script>
</body>
</html>
输出:
JavaScript Objects Example
DelftStack is a technology company registered in the USA with id: 571
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。