Problems constructor 229 ES5

The constructor method is useful, but the problem there is a waste of memory.

    function Star(uname, age) {
        this.uname = uname;
        this.age = age;
        this.sing = function(song) {
            console.log(`${this.uname}唱${song}`);
        }
    }

    var s1 = new Star('刘德华', 11);
    var s2 = new Star('张学友', 22);
    console.log(s1.sing === s2.sing);  // false,说明实例对象的sing方法并不是同一个

Guess you like

Origin www.cnblogs.com/jianjie/p/12219340.html