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
Release: 2024-12-06 05:01:14
Original
681 people have browsed it

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);
    }
};
Copy after login

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]) {}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template