JavaScript定义类的方法

来源:互联网 发布:ubuntu的浏览器字体 编辑:程序博客网 时间:2024/06/11 22:56
//混合的构造函数/原型方式//即使用构造函数定义对象的所有非函数属性,用原型方式定义定义对象的函数属性。//结果是所有函数都只创建一次,每个对象都有自己的对象属性实例//function Car(sColor,iDoor,iMpg,){//    this.color = sColor;//    this.doors = iDoor;//    this.mpg = iMpg;//    this.driver = new Array("Mike","John");//}////Car.prototype.showColor = function(){//    alert(this.color);//}////var car1 = new Car("red",4,23);//var car2 = new Car("blue",5,25);////car1.driver.push("Kelsey");////alert(car1.driver);//alert(car2.driver);////动态原型方法,与混合法主要区别在于赋予对象方法的位置。//function Car(sColor,iDoors,iMpg){//    this.color =sColor;//    this.doors = iDoors;//    this.mpg = iMpg;//    this.drivers = new Array("Mike","John");////    if(typeof Car._initialized == "undefined"){//        Car.prototype.showColor = function(){//            alert(this.color);//        };////        Car._initialized= true;//    }//}
原创粉丝点击