JavaScript 中的类是什么?
javascriptobject oriented programmingfront end technology更新于 2025/9/20 2:20:17
类
类是一种函数类型,但不是使用关键字"函数",而是使用关键字"类"来初始化它,并在constructor()方法中分配属性。每次初始化类对象时都会调用constructor()方法。
示例 1
在下面的例子中,创建了一个名为"公司"的类,并在constructor()方法中分配公司名称并在输出中显示结果。
<html>
<body>
<p id="class"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
}
myComp = new Company("Tutorialspoint");
document.getElementById("class").innerHTML = myComp.name;
</script>
</body>
</html>
输出
Tutorialspoint
示例 2
<html>
<body>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
}
myComp = new Company("Rk enterprises");
document.write(myComp.name);
</script>
</body>
</html>
输出
Rk enterprises
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

