| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Functions/Method_definitions | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
ECMAScript 6, . , , .
var obj = {
property([parameters]) {},
get property() {},
set property(value) {},
* generator() {}
};
:
var obj = {
foo: function () {},
bar: function () {},
};
:
var obj = {
foo() {},
bar() {},
};
- . , (*) . , * g(){} , g *(){} .
// (pre-ES6)
var obj2 = {
g: function* () {
var index = 0;
while (true) yield index++;
},
};
//
var obj2 = {
*g() {
var index = 0;
while (true) yield index++;
},
};
var it = obj2.g();
console.log(it.next().value); // 0
console.log(it.next().value); // 1
- TypeError .
var obj = {
method() {},
};
new obj.method(); // TypeError: obj.method is not a constructor
var obj = {
*g() {},
};
new obj.g(); //
var obj = {
a: "foo",
b() {
return this.a;
},
};
console.log(obj.b()); // "foo"
.
var bar = {
foo0: function () {
return 0;
},
foo1() {
return 1;
},
["foo" + 2]() {
return 2;
},
};
console.log(bar.foo0()); // 0
console.log(bar.foo1()); // 1
console.log(bar.foo2()); // 2
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-method-definitions |
This page was last modified on 4 . 2023 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |