JavaScript 是基於原型的語言,理解 `prototype`、`constructor`、`__proto__` 及繼承機制是掌握 JS 面向對象的鑰匙。本文通過代碼示例系統講解:構造函數、原型對象、繼承模式、靜態/實例成員、對象字面量等核心概念。
function Person(name) {
// 實例屬性
this.name = name;
// 實例方法(不推薦:每個實例創建新函數)
this.sayName = function() {
console.log(this.name);
};
}
// 原型方法(推薦:所有實例共享)
Person.prototype.sayHi = function() {
console.log('Hi, I am ' + this.name);
};
// 靜態屬性/方法(屬於構造函數本身)
Person.species = 'Human';
Person.createAnonymous = function() {
return new Person('Anonymous');
};
var p1 = new Person('Alice');
var p2 = new Person('Bob');
p1.sayHi(); // Hi, I am Alice
p2.sayHi(); // Hi, I am Bob
console.log(Person.species); // Human
console.log(Person.createAnonymous().name); // Anonymous
// p1.__proto__ === Person.prototype
// Person.prototype.__proto__ === Object.prototype
// Object.prototype.__proto__ === null
console.log(p1.__proto__ === Person.prototype); // true
console.log(Person.prototype.__proto__ === Object.prototype); // true
function Parent() {
this.colors = ['red', 'blue'];
}
Parent.prototype.getColors = function() {
return this.colors;
};
function Child() {
// 必須調用父構造函數以繼承實例屬性
Parent.call(this); // 盜用構造函數
}
// 關鍵:子類原型指向父類實例
Child.prototype = new Parent();
// 修正 constructor 指向
Child.prototype.constructor = Child;
// 新增子類方法
Child.prototype.say = function() {
console.log('Child');
};
var c1 = new Child();
c1.colors.push('green');
console.log(c1.getColors()); // ['red', 'blue', 'green']
var c2 = new Child();
console.log(c2.getColors()); // ['red', 'blue'] —— 引用類型屬性不再共享
function Parent(name) {
this.name = name;
this.friends = ['Mike'];
}
Parent.prototype.sayName = function() {
console.log(this.name);
};
function Child(name, age) {
// 繼承實例屬性(第一次調用 Parent)
Parent.call(this, name);
this.age = age;
}
// 繼承原型方法(第二次調用 Parent,但不傳參)
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.prototype.sayAge = function() {
console.log(this.age);
};
var child = new Child('Tom', 10);
child.sayName(); // Tom
child.sayAge(); // 10
console.log(child.friends); // ['Mike'] —— 獨立副本
function inheritPrototype(subType, superType) {
var prototype = Object.create(superType.prototype); // 創建對象
prototype.constructor = subType; // 增強對象
subType.prototype = prototype; // 指定對象
}
// 使用
inheritPrototype(Child, Parent);
var obj = { a: 1 };
Object.prototype.b = 2;
console.log('a' in obj); // true(自有+繼承)
console.log(obj.hasOwnProperty('a')); // true(僅自有)
console.log('b' in obj); // true
console.log(obj.hasOwnProperty('b')); // false
// 遍歷自有屬性
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key, obj[key]);
}
}
// ES5 靜態方法
console.log(Object.keys(obj)); // ['a']
console.log(Object.getOwnPropertyNames(obj)); // ['a']
// 對象字面量
var user = {
name: 'Jack',
age: 25,
greet: function() {
console.log('Hello, ' + this.name);
}
};
// 動態添加/刪除
user.email = 'jack@example.com';
delete user.age;
// 方法簡寫(ES6)
var obj = {
foo() { console.log('foo'); },
bar: function() { console.log('bar'); }
};
JavaScript 的原型繼承雖與傳統類繼承不同,但靈活強大。深入理解原型鏈、構造函數、原型對象三者關係,配合組合繼承或 ES6 class,能編寫出清晰、高效、可維護的面向對象代碼。