zoukankan      html  css  js  c++  java
  • js原型二

     1 function Box(name,age){
     2 
     3    this.name = name;
     4 
     5    this.age = age;
     6 
     7    this.family = ['哥哥',‘姐姐’,‘妹妹’];
     8 
     9 }
    10 
    11 Box.prototype = {
    12 
    13    constructor:Box,
    14 
    15    run:functiong(){
    16    return this.name+this.age+"运行中"
    17 
    18   }
    19 }
    20 var box1 = new Box("Lee",100);
    21 
    22 var box2 = new Box("Jack",200);

    组合构造函数+原型模式

    使用动态原型模式

     1 function Box(name,age){
     2    this.name = name;
     3    this.age = age;
     4   this.family = ['哥哥',‘姐姐’,‘妹妹’];
     5   if(typeof this.run !="function"){          //判断this.run是否存在
     6        Box.prototype.run=function(){
     7           return this.name+this.age+"运行中"
     8  }          
     9  }
    10  }
    11 //原型的初始化只要第一次初始化就可以了,没必要每次构造函数实例化的时候都初始化。
    12  var box1 = new Box("Lee",100)
    13  var box2 = new Box("Jack",200)
  • 相关阅读:
    第八次作业
    第七次作业2
    jsp第十一次作业
    jsp第十次作业
    jsp第九次作业
    jsp第八次作业
    jsp第七次作业
    jsp第六次作业
    软件测试第二次作业
    jsp第五次作业
  • 原文地址:https://www.cnblogs.com/bhan/p/5511595.html
Copyright © 2011-2022 走看看