Basic Object Oriented JavaScript
了解元件、物件、屬性、方法、事件的定義。
by Joseph Chiang
JavaScript
// 元件
function People(name, weight, height) {
this.name = name || "Anonymous"; // 屬性
this.weight = weight || "65kg"; // 屬性
this.height = height || "170cm"; // 屬性
}
// 用 prototype 設定方法可以共用、減少重複
People.prototype.introduce = function () {
alert("I am " + this.name + ".\n" +
"My weight is " + this.weight + ".\n" +
"My height is " + this.height + ".");
};
// 物件 1
var josephj = new People("Awoo", "178cm", "75kg");
josephj.introduce();
// 物件 2
var knock = new People("Knock", "175cm", "72kg");
knock.introduce();