Home >Web Front-end >JS Tutorial >How Can Object Methods Be Defined in JavaScript Without the '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!