console.log(this === window); // true (非嚴格模式)
console.log(this.alert === window.alert); // true
console.log(this.parseInt('10', 10)); // 10
function foo() {
console.log(this.fruit);
}
var fruit = 'global';
var pack = {
fruit: 'pack',
foo: foo
};
foo(); // 'global' —— 普通調用,this 指向 window
pack.foo(); // 'pack' —— 方法調用,this 指向 pack
foo.call(pack); // 'pack' —— 显式綁定
foo.apply(pack); // 'pack'
var obj = {
fruit: 'arrow',
normal: function() {
console.log('normal:', this.fruit); // 'arrow'
var arrow = () => console.log('arrow:', this.fruit); // 繼承外層 this
arrow();
}
};
obj.normal();
// normal: arrow
// arrow: arrow
function Person(name) {
this.name = name;
}
var p = new Person('Tom');
console.log(p.name); // Tom —— this 指向新創建的實例
function Foo() {}
console.log(typeof Foo.prototype); // 'object'
console.log(Foo.prototype.constructor === Foo); // true
function Person(name) {
this.name = name;
}
Person.prototype.sayHi = function() {
console.log('Hi, ' + this.name);
};
var p1 = new Person('Alice');
var p2 = new Person('Bob');
p1.sayHi(); // Hi, Alice
p2.sayHi(); // Hi, Bob
console.log(p1.__proto__ === Person.prototype); // true
console.log(Person.prototype.__proto__ === Object.prototype); // true
// 為 Array 添加 min 方法
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
console.log([5, 2, 9, 1].min()); // 1
// ⚠️ 陷阱:for...in 會遍歷出擴展方法
var arr = [1,2,3];
for (var key in arr) {
console.log(key); // '0','1','2','min' 😱
}
// 解決:使用 hasOwnProperty 或 Object.keys / for...of
for (var key of Object.keys(arr)) { console.log(key); } // '0','1','2'
var arr = [1,2,3];
console.log(arr.constructor === Array); // true
function Foo() {}
console.log(Foo.constructor === Function); // true (函數是 Function 的實例)
var obj = new Foo();
console.log(obj.constructor === Foo); // true
console.log(obj.constructor.constructor === Function); // true
function Person(name) {
this.name = name;
}
// ❌ 直接覆蓋 prototype 對象
Person.prototype = {
getName: function() { return this.name; }
};
var p = new Person('Tom');
console.log(p.constructor === Person); // false! 指向 Object
console.log(Person.prototype.constructor === Object); // true
Person.prototype = {
getName: function() { return this.name; }
};
// 手動修正
Person.prototype.constructor = Person;
var p = new Person('Tom');
console.log(p.constructor === Person); // true
// 方式一:賦值單個屬性(不覆蓋整個 prototype)
Person.prototype.getName = function() { return this.name; };
Person.prototype.setName = function(n) { this.name = n; };
// 方式二:Object.assign 合併(保留原 constructor)
Object.assign(Person.prototype, {
getName: function() { return this.name; },
setName: function(n) { this.name = n; }
});
// constructor 自動保持指向 Person
┌─────────────────────┐
│ Function.prototype│ ◄── 所有函數的 __proto__
└──────────┬──────────┘
│
┌───────────┴───────────┐
│ │
┌────┴────┐ ┌────┴────┐
│ Person │ │ Array │ (構造函數)
└────┬────┘ └────┬────┘
│ │
│ .prototype │ .prototype
▼ ▼
┌─────────────┐ ┌─────────────┐
│Person.prototype│ │Array.prototype│ (原型對象)
│.constructor│ │.constructor│
│ ───┬─────►│ │ ───┬─────►│
└─────│──────┘ └─────│──────┘
│ │
│ new │ new
▼ ▼
┌──────────┐ ┌──────────┐
│ p1 │ │ arr │ (實例)
└──────────┘ └──────────┘
│ │
│ __proto__ │ __proto__
└─────────┬─────────┘
▼
┌─────────────┐
│Object.prototype│
└─────────────┘
`this`、`prototype`、`constructor` 是 JavaScript 面向對象的鐵三角。徹底理解它們的動態綁定、原型鏈查找、構造指向機制,是從「會用 JS」進階到「精通 JS」的必經之路。