方法定义 - JavaScript

方法定义 - JavaScript

试一试

const obj = {

foo() {

return "bar";

},

};

console.log(obj.foo());

// Expected output: "bar"

语法

js({

property(parameters) {},

*generator(parameters) {},

async property(parameters) {},

async *generator(parameters) {},

// with computed keys

[expression](parameters) {},

*[expression](parameters) {},

async [expression](parameters) {},

async *[expression](parameters) {},

})

描述

这种简写语法类似于getter和setter语法。

给定以下代码

jsconst obj = {

foo: function () {

// …

},

bar: function () {

// …

},

};

你现在可以将其缩短为

jsconst obj = {

foo() {

// …

},

bar() {

// …

},

};

使用此语法定义的属性是创建的对象的自身属性,并且它们是可配置、可枚举和可写的,就像普通属性一样。

function*、async function和async function*属性都有各自的方法语法;请参见下面的示例。

但是,请注意,方法语法不等同于一个值为函数的普通属性——它们在语义上存在差异。这使得对象字面量中定义的方法与类中的方法更加一致。

方法定义不可构造

方法不能作为构造函数!如果你尝试实例化它们,它们将抛出TypeError。另一方面,作为函数创建的属性可以用作构造函数。

jsconst obj = {

method() {},

};

new obj.method(); // TypeError: obj.method is not a constructor

在方法定义中使用 super

只有定义为方法才能够访问super关键字。super.prop会在方法初始化的对象的原型上查找属性。

jsconst obj = {

__proto__: {

prop: "foo",

},

notAMethod: function () {

console.log(super.prop); // SyntaxError: 'super' keyword unexpected here

},

};

示例

使用方法定义

jsconst obj = {

a: "foo",

b() {

return this.a;

},

};

console.log(obj.b()); // "foo"

类中的方法定义

你可以使用完全相同的语法来定义类实例上可用的公共实例方法。在类中,方法之间不需要逗号分隔符。

jsclass ClassWithPublicInstanceMethod {

publicMethod() {

return "hello world";

}

secondPublicMethod() {

return "goodbye world";

}

}

const instance = new ClassWithPublicInstanceMethod();

console.log(instance.publicMethod()); // "hello world"

公共实例方法定义在类的prototype属性上,因此由类的所有实例共享。它们是可写的、不可枚举的和可配置的。

在实例方法内部,this和super像普通方法一样工作。通常,this指向实例本身。在子类中,super允许你访问方法所附加的对象的原型,从而可以调用超类中的方法。

jsclass BaseClass {

msg = "hello world";

basePublicMethod() {

return this.msg;

}

}

class SubClass extends BaseClass {

subPublicMethod() {

return super.basePublicMethod();

}

}

const instance = new SubClass();

console.log(instance.subPublicMethod()); // "hello world"

静态方法和私有方法使用类似的语法,这在static和私有元素页面中描述。

计算属性名

方法语法还支持计算属性名。

jsconst bar = {

foo0: function () {

return 0;

},

foo1() {

return 1;

},

["foo".toUpperCase()]() {

return 2;

},

};

console.log(bar.foo0()); // 0

console.log(bar.foo1()); // 1

console.log(bar.FOO()); // 2

生成器方法

请注意,生成器方法语法中的星号 (*) 必须在生成器属性名*之前*。(也就是说,* g(){}会起作用,但g *(){}不会。)

js// Using a named property

const obj = {

g: function* () {

let index = 0;

while (true) {

yield index++;

}

},

};

// The same object using shorthand syntax

const obj2 = {

*g() {

let index = 0;

while (true) {

yield index++;

}

},

};

const it = obj2.g();

console.log(it.next().value); // 0

console.log(it.next().value); // 1

异步方法

js// Using a named property

const obj = {

f: async function () {

await somePromise;

},

};

// The same object using shorthand syntax

const obj2 = {

async f() {

await somePromise;

},

};

异步生成器方法

js// Using a named property

const obj = {

f: async function* () {

yield 1;

yield 2;

yield 3;

},

};

// The same object using shorthand syntax

const obj2 = {

async *f() {

yield 1;

yield 2;

yield 3;

},

};

规范

规范

ECMAScript® 2026 语言规范# sec-method-definitions

浏览器兼容性

加载中…

另见

使用对象指南

函数

get

set

对象初始化器

class

帮助改进 MDN

此页面对您有帮助吗?

了解如何贡献 此页面最后由MDN 贡献者在2025 年 7 月 8 日修改。

在 GitHub 上查看此页面 • 报告此内容的问题

相关推荐

《鲁迅全集》主要版本梳理
365现在还能安全提款吗

《鲁迅全集》主要版本梳理

📅 10-26 👁️ 7879
电子秤称数量怎么设置(电子秤数量设置方法)
Bet体育365怎么提款

电子秤称数量怎么设置(电子秤数量设置方法)

📅 08-04 👁️ 3106
乌拉圭:从历史冠军到现代强队的传奇之旅
365现在还能安全提款吗

乌拉圭:从历史冠军到现代强队的传奇之旅

📅 09-25 👁️ 521
2017年驾考新政策!科目一、科目二、科目三新规变化在哪里?
肥肠生的还是煮好保存?肥肠怎样保存最新鲜
365现在还能安全提款吗

肥肠生的还是煮好保存?肥肠怎样保存最新鲜

📅 09-11 👁️ 1636
醤的意思,醤的解释,醤的拼音,醤的部首
Bet体育365怎么提款

醤的意思,醤的解释,醤的拼音,醤的部首

📅 08-02 👁️ 9423
轩尼诗VSOP储存期多久,轩尼诗VSOP的保质期有多长?
Bet体育365怎么提款

轩尼诗VSOP储存期多久,轩尼诗VSOP的保质期有多长?

📅 11-20 👁️ 5484
最值得入手的aj
365现在还能安全提款吗

最值得入手的aj

📅 09-04 👁️ 464
捕蝇草的生长过程 – 知花者
Bet体育365怎么提款

捕蝇草的生长过程 – 知花者

📅 07-29 👁️ 7738