• 构造函数创建对象


    <script type="text/javascript">
            function Car(make,model,year,color,passengers,convertible,mileage){
                this.make=make;
                this.model=model;
                this.year=year;
                this.color=color;
                this.passengers=passengers;
                this.convertible=convertible;        
                this.mileage=mileage;
                this.started=false;//将属性started初始化false
                this.start=function(){
                    this.started=true;
                };
                this.stop=function(){
                    this.started=false;
                };
                this.drive=function(){
                    if (this.started) {
                        console.log(this.make+" " +this.model  +" does zoom zoom");
                    } else {
                        alert("you are first");
                    }
                };
    
            }
            var chevy1= new Car("chevy1","bel air1",1967,"red",2,false,20121);
            var chevy2= new Car("chevy2","bel air2",1967,"red",2,false,20122);
            var chevy3= new Car("chevy3","bel air3",1967,"red",2,false,20123);
            var chevy4= new Car("chevy4","bel air4",1967,"red",2,false,20124);
            var cars =  [chevy1,chevy2,chevy3,chevy4];
            for (var i = 0; i < cars.length; i++) {
                cars[i].start();
                cars[i].drive();
              
                cars[i].stop();
            }
        </script>

    注意:构造函数命名是使用的是首字母大写,目的是为了区分普通函数   

       属性名和形参名不必相同,但通常相同 ,这是约定

       构造函数不用返回值return 本身会完成这项任务

            先有构造函数  再有对象

  • 相关阅读:
    oracle中常用的函数
    请求转发和URL重定向的原理和区别
    servlet的生命周期和servlet的继承关系
    Jdbc来操作事物 完成模拟银行的转账业务
    Map的嵌套 练习
    正则表达式练习
    学习 day4 html 盒子模型
    学习day03
    学习day02
    学习day01
  • 原文地址:https://www.cnblogs.com/xiaoxiao2017/p/7987387.html
Copyright © 2020-2023  润新知