Home >Web Front-end >JS Tutorial >How Can Object Methods Be Defined in JavaScript Without the 'function' Keyword?

How Can Object Methods Be Defined in JavaScript Without the 'function' Keyword?

Susan Sarandon
Susan SarandonOriginal
2024-12-06 05:01:14682browse

How Can Object Methods Be Defined in JavaScript Without the

Object Method Definition without "function" Keyword

Question:

How is the following object method definition possible without the "function" keyword?

var module = {
    foobar(arg1) {
        alert(arg1);
    }
};

Answer:

This method definition is possible due to a feature introduced in ECMAScript 6 (ES6).

The ES6 standard defines a shorthand notation for method definitions, eliminating the need for the "function" keyword. This notation is as follows:

property([parameters]) {}

In the provided code, the "foobar" method is defined without the "function" keyword, utilizing the ES6 shorthand notation. This is recognized by some browsers, including Chrome, allowing the method to execute successfully.

IE 11.0.9600.17959, however, does not support this notation, causing the method definition to fail.

The above is the detailed content of How Can Object Methods Be Defined in JavaScript Without the 'function' Keyword?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn