手册
目录
收藏698
分享
阅读599
更新时间2025-08-07
class Car {
constructor(name) {
this.name = name;
}
static hello() {
return "Hello!!";
}
}
let myCar = new Car("Ford");
// 您可以在 Car 类上调用 'hello()' :
document.getElementById("demo").innerHTML = Car.hello();
// 但不能在 Car 对象上调用:
// document.getElementById("demo").innerHTML = myCar.hello();
// 此举将引发错误。
运行实例 »点击 "运行实例" 按钮查看在线实例
如果要在 static 方法中使用 myCar 对象,可以将其作为参数发送:
class Car {
constructor(name) {
this.name = name;
}
static hello(x) {
return "Hello " + x.name;
}
}
let myCar = new Car("Ford");
document.getElementById("demo").innerHTML = Car.hello(myCar);
运行实例 »点击 "运行实例" 按钮查看在线实例
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
70.9万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习