In this example, whensayHello()is called, it executes the arrow function which returns the stringHello, World!. Example 2: Arrow Function With One Argument If a function has only one argument, you can omit the parentheses. For example, constsquare =x=>x * x; // use the arrow function ...
c: function() { console.log(this.i, this); } } obj.b(); // prints undefined, Window {...} (or the global object) obj.c(); // prints 10, Object {...} 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. Arrow functions do not have their own this. Another example involving 箭...
function example(a: number, b: number) { return a + b } 同样arguments 的类型也有强制要求; Arrow Function 也是如此: const example = (a: number, b: number) => ( a + b );
The first example uses a regular function, and the second example uses an arrow function. 第一个示例使用常规函数,第二个示例使用箭头函数。 The result shows that the first example returns two different objects (window and button), and the second example returns the window object twice, because t...
// A button object calls the function: document.getElementById("btn").addEventListener("click", hello); Try it Yourself » Example With an arrow functionthisrepresents theownerof the function: // Arrow Function: hello = () => {
createArrowFunction: function() { return () => { console.log(this.name); console.log(arguments); }; } }; 我们有一个简单的test对象,有两个方法 – 每个方法都返回一个匿名函数。 不同之处在于第一个方法使用传统函数表达式,而后者中使用箭头函数。
createArrowFunction: function() { return () => { console.log(this.name); console.log(arguments); }; } }; 我们有一个简单的 test 对象,有两个方法 – 每个方法都返回一个匿名函数。 不同之处在于第一个方法使用传统函数表达式,而后者中使用箭头函数。
Example // create a function named greet()functiongreet(){console.log("Hello World!"); }// store a function in the displayPI variable// this is a function expressionletdisplayPI =function(){console.log("PI = 3.14"); }// call the greet() functiongreet();// call the reply() functio...
可以看到,用箭头函数方式定义的arrowFn被放到实例ins中去了, 而用普通函数方式定义的commonFn,则是和constructor一起,放到了ArrowExample的原型当中。 S2.箭头函数无法实例化 运行如下代码 constf=()=>{};newf(); 结果 S3.箭头函数没有自己的this,还有arguments 要上溯 ...
asyncfunctionfetchData(){try{constresponse=awaitfetch('https://api.example.com/data');constdata=awaitresponse.json();returndata;}catch(error){console.error('Error:',error);}} async/await让异步代码看起来像同步代码,这有助于提高代码的可维护性。