日期:2014-05-16 浏览次数:20363 次
function createCar(sColor, iDoors, iMpg){ var oTempCar = new Object oTempCar.color = sColor oTempCar.doors = iDoors oTempCar.mpg = iMpg oTempCar.showColor = function(){ alert(this.color) } return oTempCar }
var car1 = createCar("red", 2, 23) var car2 = createCar("blue", 4, 30) alert(car1.showColor==car2.showColor) //显示“false”,表示这2个function不是同一个实例。
function Car(sColor, iDoors, iMpg){ this.color = sColor this.doors = iDoors this.mpg = iMpg this.showColor = function(){ alert(this.color) } }
function Car(){ } Car.prototype.color = "red" Car.prototype.doors = 4 Car.prototype.mpg = 23 Car.prototype.showColor = function(){ alert(this.color) }
function Car(){ } Car.prototype.color = "red" Car.prototype.doors = 4 Car.prototype.mpg = 23 Car.prototype.drivers = new Array("Mike", "Sue"); Car.prototype.showColor = function(){ alert(this.color) } var car1 = new Car var car2 = new Car car1.drivers.push("Matt") alert(car2.drivers)
function Car(sColor, iDoors, iMpg){ this.color = sColor this.doors = iDoors this.mpg = iMpg this.drivers = new Array("Mike", "Sue") } Car.prototype.showColor = function(){ alert(this.color) }
function Car(sColor, iDoors, iMpg){ this.color = sColor this.doors = iDoors this.mpg = iMpg this.drivers = new Array("Mike", "Sue") if(!Car._initialized){ Car.prototype.showColor = function(){ alert(this.color) } Car._initialized = true } }