zoukankan      html  css  js  c++  java
  • js类 混合的构造函数/原型模式

    混合的构造函数/原型模式

    联合使用构造函数和原型方式,定义类就非常方便。

    <script type="text/javascript">
    //定义
        function Car(color,doors)
       {
            this.color=color;
            this.doors=doors;
            this.drivers=new Array("Tom","Jerry");
       }

       Car.prototype.showColor=function(){
            alert(this.color);
       }
       
       //调用:
       var car1=new Car('red',4);
       var car2=new Car('blue',4);
       
       car1.showColor();
       car2.showColor();
       
       alert(car1.drivers);
       car1.drivers.push("stephen");
       alert(car1.drivers); //结果:Tom,Jerry,stephen
       alert(car2.drivers); //结果:Tom,Jerry
       alert(car1 instanceof Car);

    </script>

  • 相关阅读:
    vue.api
    v-resource
    vue.js路由
    computed watch methods
    vue.js生命周期
    flex布局
    字符截取 slice substr substring
    原生Ajax书写
    jq动画
    css 3动画
  • 原文地址:https://www.cnblogs.com/xiangniu/p/2077892.html
Copyright © 2011-2022 走看看