Skip to main content

ES6 arrow functions

 In ES6, in addition to the new features of let and const, arrow functions are the most frequently used new features. But this feature is not unique to ES6. Arrow functions, as the name suggests, are functions defined using arrows (=>) and belong to a class of anonymous functions. It allows us to write smaller function syntaxes. The code of arrow functions is simpler and more flexible to write.

The content of the article will introduce arrow functions from the following aspects:

  • Basic usage
  • specific characteristics

Basic usage:
  • Declarative functions cannot be abbreviated, only function expressions can be abbreviated.
let fun1=function(a,b){};
  let fun2=(a,b)=>{};
  let obj={
    fun1:function(a,b){},
    fun2:(a,b)=>{}
  }
  • When there is only one formal parameter of the function, you can not write () in other cases, you must write.
let fun=a=>{console.log("1111")}
  • Omit curly braces. When there is only one statement in the code body, return must be omitted.
  let fun3=a=>console.log("666");
  let fun4=b=>b+b;

specific characteristic:
  • this is static. this always points to the context's this.
  window.id=500;
  let id=100;//var id=100,then it will overwrite the value of window
  let obj={
    id:200
  }
  let fun1=function(){
    console.log(this.id,this);
  }
  let fun2=()=>{
    console.log(this.id,this);
  }
  fun1.call(obj);//200,{id:200}
  fun2.call(obj);//500,Window
  var id=500;//or window.id=500;
  let obj={
    id:100,
    fun1:function(){
        console.log(this.id,this);
    },
    fun2:()=>{
        console.log(this.id,this);
    }
  }
  obj.fun1();//100,object....
  obj.fun2();//500,Window
  • cannot be used as a constructor
  let A=(age,name)=>{
    this.age=age;
    this.name=name;
  }
  let a=new A(10,"aaa");//Uncaught TypeError: A is not a constructor
  • Can not be used as an arrow function inside the parameter collection constructor without arguments.
  let fun1=function(){
    console.log(arguments);
  }
  let fun=()=>{
    console.log(arguments);
  }
  fun1(100,500,800);//[100,500,800]
  fun(10,20,30);//Uncaught ReferenceError: arguments is not defined at fun
  • Arrow functions do not have a prototype.
  let funy=(a,b)=>{
    console.log(funy.prototype);
  }
  funy();//undefined






Comments

Popular posts from this blog

What is the difference between width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.