本文解析 javascript 类中同名字段(如 `method = "sss"`)与方法(如 `method() {}`)在继承时的行为差异:前者作为实例自有属性被初始化,后者定义在原型上;当子类重写同名方法时,实例属性优先于原型方法,导致看似“覆盖失效”的现象。
在 JavaScript 类中,看似相同的名称(如 method)可能代表完全不同的语言特性——类字段(Class Fields) 与 类方法(Class Methods),它们在内存布局、属性归属和继承行为上存在本质区别。

类字段语法(method = "sss") 是 constructor 中赋值的语法糖:
class Parent {
method = "sss"; // 等价于 constructor() { this.method = "sss"; }
}✅ 它会在每次 new Parent() 或 new Child() 时,直接在实例对象上创建一个自有属性(own property)。
✅ 因此 child.hasOwnProperty("method") 返回 true。
类方法语法(method() { ... }) 则被定义在类的 prototype 上:
class Parent {
method() { console.log("Parent"); }
}
// 等价于 Parent.prototype.method = function() { ... };❌ 它不会出现在实例自身上,而是通过原型链访问。
❌ child.hasOwnProperty("method") 返回 false,但 Parent.prototype.hasOwnProperty("method") 为 true。
看这个关键示例:
class Parent {
method = "sss"; // ← 实例属性:child.method = "sss"
}
class Child extends Parent {
method() { console.log("Child"); } // ← 原型方法:Child.prototype.method
}
const child = new Child();
console.log(child.method); // 输出: "sss"执行过程如下:
⚠️ 注意:若尝试调用 child.method(),将抛出 TypeError: child.method is not a function。
class Parent {
prop = "parent value";
method() { console.log("Parent"); }
}
class Child extends Parent {
prop() { return "child value"; }
method() { console.log("Child"); }
}
const child = new Child();
console.log(child.hasOwnProperty("prop")); // true ← 字段:实例自有
console.log(child.hasOwnProperty("method")); // false ← 方法:在原型上
console.log(Child.prototype.hasOwnProperty("prop")); // false
console.log(Child.prototype.hasOwnProperty("method")); // true
console.log(Parent.prototype.hasOwnProperty("method")); // true此时 child 的完整结构等效于:
{
prop: "parent value", // ← 来自 Parent 构造器赋值(类字段)
__proto__: {
prop() { return "child value"; },
method() { console.log("Child"); },
__proto__: {
method() { console.log("Parent"); } // ← Parent.prototype
}
}
}理解类字段与方法的底层机制,是写出可维护、可预测的 JavaScript 类的关键一步。