+
95
-

js中箭头函数与普通函数有什么区别?

请问js中箭头函数与普通函数有什么区别?

网友回复

+
15
-

普通函数格式: function add(a,b){return a+b};

箭头函数格式:add =(a,b)=>a+b;或add=(a,b)=>{return a+b;} 这里就不详细说明箭头函数的简写规则,2者区别主要分为以下几个方面: 1.this方面: 普通函数内部的this,默认指向window,严格模式下指向undefined;

箭头函数内部的this,与上一级作用域中的this指向同一个地方。

具体示例代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <script type="text/javascript">

        var person = {
            name: "小明",
            age: 2,
            sayname: function() {
                setTimeout(function() {
                    console.log...

点击查看剩余70%

我知道答案,我要回答