<!--js类
使用class创建类:
语法:constructor语法,ClassName类名
class ClassName {
constructor() { ... }
}
-->
<p id="demo"></p>
<script>
let Runoob = class {
constructor(name, url) {
this.name = name;
this.url = url;
}
};
console.log(Runoob.constructor.name);//返回类型
// output: "Runoob"
// 命名类
let Runoob2 = class Runoob2 {
constructor(name, url) {
this.name = name;
this.url = url;
}
};
console.log(Runoob2.name);
// 输出: "Runoob2"
class Runoob3 {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
let date = new Date();
return date.getFullYear() - this.year;
}
}
let runoob = new Runoob3("菜鸟教程", 2018);
document.getElementById("demo").innerHTML =
"菜鸟教程 " + runoob.age() + " 岁了。";
//constructor属于构造方法,如果不定义构造方法,那么会自动生成一个构造方法,构造方法在创建新对象时会自动执行,构造方法用于初始化对象属性,属于age方法可以调用构造方法的属性
//眼睛酸,不太静
标签:静态方法,name,构造方法,url,创建,year,JS,constructor,class From: https://www.cnblogs.com/H-Yan/p/16925653.html