[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Functions/Method_definitions [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

ECMAScript 6, . , , .

In this article

var obj = {
  property([parameters]) {},
  get property() {},
  set property(value) {},
  * generator() {}
};

getter' setter' ECMAScript 5.

:

js
var obj = {
  foo: function () {},
  bar: function () {},
};

:

js
var obj = {
  foo() {},
  bar() {},
};

-

- . , (*) . , * g(){} , g *(){} .

js
//     (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

(ES6)

- TypeError .

js
var obj = {
  method() {},
};
new obj.method(); // TypeError: obj.method is not a constructor

var obj = {
  *g() {},
};
new obj.g(); // 

js
var obj = {
  a: "foo",
  b() {
    return this.a;
  },
};
console.log(obj.b()); // "foo"

.

js
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


Web Proxy Viewer  |  New URL  |  Original Page