对象与原型链
约 262 字小于 1 分钟
布欧-Lewyon
2026-05-16
首页 › JavaScript › 对象与数组 › 对象与原型链
对象基础
// 字面量创建
const user = {
name: '张三',
age: 28,
greet() {
console.log(`你好,我叫${this.name}`);
}
};
// 属性访问
user.name; // "张三"
user['age']; // 28
user.greet(); // "你好,我叫张三"
// 动态属性
const key = 'email';
user[key] = 'zhang@example.com';
// 属性枚举
Object.keys(user); // ["name", "age", "greet", "email"]
Object.values(user); // ["张三", 28, fn, "zhang@..."]
Object.entries(user); // [["name","张三"], ...]原型与原型链
const arr = [1, 2, 3];
// arr → Array.prototype → Object.prototype → null
const obj = { name: '张三' };
// obj → Object.prototype → null
function Person() {}
const p = new Person();
// p → Person.prototype → Object.prototype → null
// instanceof 沿着原型链查找
console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // true
console.log(arr instanceof Person); // false小结
| 概念 | 要点 |
|---|---|
| 对象 | 键值对集合,属性动态增删 |
| 原型链 | __proto__ 串联,属性查找沿链向上 |
instanceof | 沿原型链检测类型 |
