您的位置:首页 > 移动开发 > Objective-C

Defining Classes and Objects

2012-07-17 12:04 267 查看
1. Factory:

function showColor(color) {

alert(this.color);

}

function createCar(color, doors, mpg) {

var a = new Object();

a.color = color;

a.doors = doors;

a.mpg = mpg;

a.showColor = showColor;

return a;

}

2.Constructor:

function Car(door, color, mpg) {

this.door = door;

this.color = color;

this.mpg = mpg;

this.showColor = function () {

alert(this.color);

}

}

var oCar1 = new Car("red", 4, 23);

var oCar2 = new Car("blue", 3, 25);

oCar1.showColor(); //outputs "red"

oCar2.showColor(); //outputs "blue"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: