$(function(){ $('#clickMe').click(()=>{alert('Hello,Arrow functions!')}); }) 上面的例子比较简单,再来看一个Promise链的例子: functiongetUserInfo(id){returngetUsers(id).then(function(users){returnusers[0]}).then(checkUser).then(function(user,userInfo){returnuserInfo }). catch(function(e...
在JavaScript代码中,函数无处不在。假设页面上有一个特定的按钮,它的id是‘clickMe’,点击它后,页面弹出“Hello,Arrow functions!”,为实现这个效果,我们会像下面这样编写JavaScript代码: $(function(){ $('#clickMe').click(function(){ alert('Hello,Arrow functions!'); }) }) 1. 2. 3. 4. 5. 以上...
arrow function 这个恐怕是ES6最最常用的一个新特性了,用它来写function比原来的写法要简洁清晰很多: function(i){returni + 1; }//ES5(i)=> i + 1//ES6 简直是简单的不像话对吧… 如果方程比较复杂,则需要用{}把代码包起来: function(x, y) { x++; y--;returnx +y; } (x, y)=> {x++;...
I'm using es2015 Arrow functions in a React component that is throwing an error when I declare myfunction() {...}. The error doesn't exist if write it like so myFunction : function () {...}. I do not have any issues compiling with Browserify/Babelify - this only happens with the...
arrow function 会让代码更简短 arrow 不会改变 this 的指向,或者说它没有自己的 this constobj={name:"tom",sayHi:function(){console.log(`hi,${this.name}`);},sayHiArrowFunc:()=>{console.log(`hi,${this.name}`);},sayHiAsync:function(){setTimeout(()=>{console.log(`hi,${this.name}`...
space-unary-ops,space-infix-ops,arrow-spacing // ✗ badi ++;letx=-y*5;letmessage='Hello, '+name+'!';constfunc=(x)=>{};// ✓ goodi++;letx = -y *5;letmessage ='Hello, '+ name +'!';constfunc = (x) => {};
3.2. 箭头函数(Arrow Functions) ES6 中,箭头函数就是函数的一种简写形式,使用括号包裹参数,跟随一个 =>,紧接着是函数体: vargetPrice =function() {return4.55; };//Implementation with Arrow FunctionvargetPrice = () => 4.55; 1. 2. 3.
prefer-arrow-callback // ✗ bad [1, 2, 3].map(function (x) { const y = x + 1; return x * y; }); // ✓ good [1, 2, 3].map((x) => { const y = x + 1; return x * y; }); 要求箭头函数参数必须使用括号。 arrow-parens // ✗ bad [1, 2, 3].map(number...
prefer-arrow-callback // ✗ bad [1, 2, 3].map(function (x) { const y = x + 1; return x * y; }); // ✓ good [1, 2, 3].map((x) => { const y = x + 1; return x * y; }); 要求箭头函数参数必须使用括号。 arrow-parens // ✗ bad [1, 2, 3].map(number...
Because when the processing of this in the arrow function is defined, the point of this is the window pointed to by the outer layer of foo, and window has no name attribute, so the result is undefined. Object 1. Concise notation of attributes let name = 'xx' let age = 18 let obj...