zoukankan      html  css  js  c++  java
  • 构造函数创建对象

    <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 本身会完成这项任务

            先有构造函数  再有对象

  • 相关阅读:
    class-dump + DumpFrameworks.pl
    使用 U盘 重装 Mac OSX
    本地化应用程序
    UINavigationBar 总结
    Xcode 杂七杂八
    App 打包并跳过 AppStore 的发布下载
    Summary of Mac Versions
    QT5安装(null)
    如何实现LAN或WAN远程开机?
    DELL 8700重装系统
  • 原文地址:https://www.cnblogs.com/xiaoxiao2017/p/7987387.html
Copyright © 2011-2022 走看看