• JS:原型


     
    function Box(){
    
    }
    
    //使用字面量的方式创建原型对象,这里{}就是对象,是Object, new Object相当于{}
    
             Box.prototype = {
                 name:"lee",
                 age:25,
                 run:function(){
                     return this.name + this.age + "运行中...";
                 }
             }
    
             
             Box.prototype = {
                 age:26 
             }
             var box1 = new Box();
             
    
    
             var a = [8,9,5,4,6,1,2];
             alert(a.sort());
    
             alert(Array.prototype.sort);
             alert(String.prototype.substring);
    
            

    2.引用类型本身使用原型,不推荐

     给String 类型添加一个方法
             String.prototype.addstring = function(){
                 return this + ",被添加了!"; 
             }
             var b = "SS";
             alert(b.addstring());
    原型的特点,原型不能传参数,原型具有共享性,也是原型的缺点
                 Box.prototype = {
                     constructor:Box,
                     name:'cc',
                     age:100,
                     family:['爸爸','妈妈','哥哥'],
                     run:function(){
                         return this.name + this.age + this.family;
                     }
                 };
    
                 var box1 = new Box();
                 alert(box1.run());  //返回“cc100 爸爸 妈妈 哥哥”
                 box1.family.push('姐姐');//返回“cc100 爸爸 妈妈 哥哥 姐姐”
                 alert(box1.run());
    
                 var box2 = new Box(); //返回“cc100 爸爸 妈妈 哥哥 姐姐”
                 alert(box2.run());    
    //为了解决构造传参合共享问题,可以组合构造函数+原型模式:
           function Box(name,age){
                     this.name = name;
                     this.age = age;
                     this.family = ["父亲","母亲", "妹妹"];
                  };
    
                  Box.prototype = {
                      constructor:Box,
                      run:function(){
                          return this.name + this.age + this.family;
                      }
                  }
    
                  var box3 = new Box();
                  alert(box3.run("cc",25)); //cc25父亲母亲妹妹
                  box3.family.push("哥哥");
                  alert(box3.run("cc",25)); //cc25父亲母亲妹妹哥哥
    
                  var box4 = new Box()
                  alert(box4.run("ll",58)); //ll58父亲母亲妹妹
    
                 
    以上方式解决了传参和共享的问题

    上面的书写方式(构造函数+原型)的写法觉得十分怪异,最好是把它们封装在一起。为了解决这个问题,可以使用动态原型模式:

    function Box(name,age){
      this.name = name;
      this.age = age;
    
      if (typeof this.run !='function') { //只在第一次调用时初始化
        Box.prototype.run= function() {
        return this.name + this.age + "正在运行中...";
       };
      };
    
    }
    
    var box5 = new Box("cc",88);
    alert(box5.run());

    ps:使用动态原型模式,要注意一点,不可以再使用字面量的方式重写原型,因为会切断实例和新原型直接的联系。

  • 相关阅读:
    ajax_注册
    mysql 二
    mysql基础
    django数据库批量创建
    私有属性
    mysql操作
    @property @classmethod @staticmethod
    python中的__new__方法
    员工信息表-装逼版
    三级菜单
  • 原文地址:https://www.cnblogs.com/wine/p/5016023.html
Copyright © 2020-2023  润新知