当前位置:网站首页>ES6_ Arrow function

ES6_ Arrow function

2022-07-07 08:28:00 bdundun

The arrow function doesn't prototype attribute , You can't use it new and arguments.
Arrow functions are not bound this keyword , In the arrow function this Points to the context where the arrow function defines the position this.

    <script>
        function Fn(){
    
            console.log(this);
            return ()=>{
    
                console.log(this);
            }
        }
        obj={
    'name':'zhangsan'}
        ret=Fn.call(obj);// Returns the arrow function 
        ret();// Call arrow function 
        /* Although the arrow function is called outside , however this No window, It is the context in which the arrow function defines the position this.*/
</script>
   <script>
        var obj={
    
            age:20,
            say:()=>{
    
                alert(this.age)
            }
        }
        obj.say();// Output undefined. Because objects cannot generate scopes , So the arrow function is defined under the global scope ,this Point to window.
    </script>
原网站

版权声明
本文为[bdundun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130640305507.html