美文网首页
ES5的类和继承

ES5的类和继承

作者: pengkiw | 来源:发表于2020-06-21 20:53 被阅读0次
    function People(name, age) {
        // 实例属性
        this.name = name;
        this.age = age;
        People.count++;
    }
    //静态属性
    People.count = 0;
    // 静态方法
    People.getCount = function() {
        console.log('当前共有' + People.count + '个人');
    }
    
    // 实例方法
    People.prototype.showName = function() {
        console.log('名字是' + this.name)
    };
    
    let p1 = new People('kiw', 18)
    p1.showName();
    console.log('p1', p1); //p1 People {name: "kiw", age: 18}
    let p2 = new People('zhangsan1', 20)
    p2.showName();
    console.log('p2', p2) // p2 People {name: "zhangsan1", age: 20}
    
    console.log(People.count) //2
    People.getCount(); // 当前共有2个人
    
    let str = new String('kiw');
    console.log(str); // String {"kiw"}  new出来的一定是对象
    console.log(typeof str); //object
    
    let arr = new Array(1, 2, 3);
    console.log(arr); // [1, 2, 3]
    
    let obj = new Object({
        name: 'kiw',
        age: '18',
    });
    console.log(obj); //{name: "kiw", age: "18"}
    

    相关文章

      网友评论

          本文标题:ES5的类和继承

          本文链接:https://www.haomeiwen.com/subject/udgaxktx.html