一、構造函數:以 “人” 為例的對象工廠
1. 生活場景下的構造函數定義
我們以 “人” 為場景創建構造函數,每個人都有姓名、年齡等個性化屬性,也有人類共有的特征:
// 人類構造函數
function Person(name, age) {this.name = name;this.age = age;// 每個人獨特的屬性this.hobby = `喜歡 ${name}的愛好`;
}// 創建兩個人的實例
const person1 = new Person("小明", 20);
const person2 = new Person("小紅", 22);console.log(person1); // Person { name: '小明', age: 20, hobby: '喜歡小明的愛好' }
console.log(person2); // Person { name: '小紅', age: 22, hobby: '喜歡小紅的愛好' }
2. 基礎數據類型的生活類比
以 “水果” 為例理解基礎數據類型的兩種定義方式:
// 定義水果的兩種方式
let fruit1 = "蘋果"; // 字面量方式
let fruit2 = new String("香蕉"); // 構造函數方式console.log(fruit1); // 蘋果
console.log(fruit2); // 香蕉
二、原型(prototype):提取人類共同特征
1. 提取人類共同屬性到原型
人類共有的屬性(如物種、默認行為)適合放在原型上:
// 優化:將人類共同屬性放到原型上
Person.prototype.species = "智人";
Person.prototype.gender = "默認未知";
Person.prototype.speak = function() {return `${this.name}說:你好!`;
};function Person(name, age) {this.name = name;this.age = age;this.hobby = `喜歡 ${name}的愛好`;
}const person1 = new Person("小明", 20);
const person2 = new Person("小紅", 22);// 訪問原型上的屬性
console.log(person1.species); // 智人
console.log(person2.speak()); // 小紅說:你好!
console.log(person1); // Person { name: '小明', age: 20, hobby: '喜歡小明的愛好' }
2. 原型屬性的不可修改性演示
嘗試修改原型屬性,觀察實例的變化:
// 原型屬性不可修改的演示
Person.prototype.species = "智人";
Person.prototype.speak = function() {return `${this.name}說:你好!`;
};function Person(name, age) {this.name = name;this.age = age;
}const person = new Person("小明", 20);console.log(person.species); // 智人
// 嘗試修改原型屬性
person.species = "外星人";
console.log(person.species); // 外星人(實例自身添加了屬性)
console.log(Person.prototype.species); // 智人(原型屬性未改變)
三、對象原型(proto):隱式鏈接的生活比喻
1. 隱式原型的鏈接關系
以 “動物” 為例說明隱式原型的指向關系:
// 動物構造函數
function Animal() {}
Animal.prototype.eat = function() {return "正在進食";
};// 狗構造函數
function Dog(name) {this.name = name;
}
// 狗的原型指向動物的原型
Dog.prototype = new Animal();const dog = new Dog("旺財");// 驗證隱式原型關系
console.log(dog.__proto__ === Dog.prototype); // true
console.log(Dog.prototype.__proto__ === Animal.prototype); // true
console.log(dog.eat()); // 正在進食(通過原型鏈調用)
2. 隱式原型的查找機制
通過 “學生” 對象演示屬性查找過程:
// 學生構造函數
function Student(name) {this.name = name;
}// 學生原型添加屬性
Student.prototype.school = "默認學校";
Student.prototype.study = function() {return `${this.name}在學習`;
};const student = new Student("小李");// 查找屬性的過程
console.log(student.name); // 小李(自身屬性)
console.log(student.school); // 默認學校(原型屬性)
console.log(student.study()); // 小李在學習(原型方法)
四、this 的原理:從 “創建房屋” 理解 new 操作
1. 房屋構造函數中的 this
模擬房屋建造過程,理解 this 在構造函數中的作用:
// 房屋構造函數
function House(owner, area) {this.owner = owner;this.area = area;this.roomCount = 3; // 默認3個房間// 這里隱含了new的執行過程// 1. 創建空對象 obj = {}// 2. this指向obj// 3. 執行屬性賦值// 4. obj.__proto__ = House.prototype// 5. 返回obj
}House.prototype.address = "未知地址";
House.prototype.showInfo = function() {return `${this.owner}的房子,面積 ${this.area}㎡,位于 ${this.address}`;
};const myHouse = new House("張三", 120);
console.log(myHouse.showInfo()); // 張三的房子,面積120㎡,位于未知地址
五、原型鏈:從 “交通工具” 看對象繼承關系
1. 交通工具的原型鏈結構
通過交通工具的層級關系展示原型鏈:
// 交通工具基類
function Vehicle() {this.type = "交通工具";
}
Vehicle.prototype.move = function() {return "正在移動";
};// 汽車類
function Car(brand) {this.brand = brand;
}
Car.prototype = new Vehicle();
Car.prototype.drive = function() {return `${this.brand}汽車在行駛`;
};// 電動車類
function ElectricCar(brand, battery) {Car.call(this, brand);this.battery = battery;
}
ElectricCar.prototype = new Car("默認品牌");
ElectricCar.prototype.charge = function() {return `正在給 ${this.brand}電動車充電`;
};// 創建電動車實例
const car = new ElectricCar("特斯拉", "鋰電池");// 原型鏈查找演示
console.log(car.brand); // 特斯拉(自身屬性)
console.log(car.drive()); // 特斯拉汽車在行駛(Car原型方法)
console.log(car.move()); // 正在移動(Vehicle原型方法)
console.log(car.charge()); // 正在給特斯拉電動車充電(自身方法)
2. 原型鏈的根對象演示
通過 Object.create 創建特殊對象:
// 普通對象
const normalObj = new Object();
console.log(normalObj.__proto__); // Object.prototype// 使用Object.create創建無原型的對象
const specialObj = Object.create(null);
console.log(specialObj.__proto__); // undefined// 驗證原型鏈終點
console.log(Object.prototype.__proto__); // null
六、生活場景總結表
概念 | 生活類比 | 代碼 |
---|---|---|
構造函數 | 工廠生產線 | 首字母大寫,用 new 創建實例 |
原型 (prototype) | 人類共同特征手冊 | 存放同類對象的公共屬性和方法 |
隱式原型 (proto) | 家族族譜 | 實例指向構造函數的原型 |
原型鏈 | 生物進化樹 | 多層繼承關系形成的屬性查找鏈路 |
this | 工廠中的施工圖紙 | 指向新創建的對象實例 |