首页 > 其他分享 >js(class)

js(class)

时间:2022-09-20 18:13:35浏览次数:53  
标签:name url site js constructor new class

1.构造函数

<script>
class Runoob{
    constructor(name,url){
        this.name = name;
        this.url = url;
    }
}
let site = new Runoob("names","hello");    //使用new关键字来创建对象,创建对象时会自动调用构造函数方法constructor():构造方法用于初始化对象属性
document.getElementById("demo").innerHTML =
site.name + ": " + site.url;
</script>
// 未命名/匿名类
<script>
let Runoobs = class {
  constructor(name, url) {
    this.name = name;
    this.url = url;
  }
};
var y = new Runoobs("jim","www")
document.write(y.url)
</script>
// 命名类
<script>
    let Runoobd = class Runoob2{
        constructor(name,url){
            this.name = name;
            this.url = url;
        }
    }
    var z = new Runoobd("hi","alibaba")
    document.write(z.name)
</script>

  

标签:name,url,site,js,constructor,new,class
From: https://www.cnblogs.com/JASON-yul/p/16711999.html

相关文章