构造函数的原型

 prototype 原型对象
 原型对象的作用就是共享方法
 function Star(uname, age) {
        this.uname = uname;
        this.age = age;
        /* this.sing = function () {
          console.log("我会唱歌,我会跳舞");
        }; */
      }
      Star.prototype.sing = function () {
        console.log("我会唱歌,我会跳舞");
      };
      var ldh = new Star("刘德华", 18);
      var zxy = new Star("张学友", 19);
      console.log(ldh.sing == zxy.sing); //false  使用原型对象后是true
 一般情况,我们的公共属性定义到构造函数里面,公共的方法我们放到原型对象身上
构造函数方法new一次就开辟一次内存空间,浪费资源内存
 
posted @ 2020-08-03 20:42  卡卡C哦  阅读(130)  评论(0)    收藏  举报