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

favicon.ico 404 Not Found Causes and Solutions

 What is favicon? It is the abbreviation of Favorites Icon. Its function is that in addition to displaying the corresponding title in the browser's favorites, icons can also be used to distinguish different websites. When I encounter this kind of error, I feel very uncomfortable. There is obviously no problem. Why is a 404 error reported? The following is the solution.

Use of custom events in Vue components.

 The parent component uses props to pass data (such as a method) to the child component, and the child component needs to use custom events to pass data back to the parent component! We can use v-on to bind custom events, and then pass them to subcomponents, which will call after receiving the method. Each Vue instance implements the Events interface, that is: use $on(eventName) to listen for events and $emit(eventName) to trigger events.

mapState, mapGetters, mapMutations, mapActions in vuex

 The above four objects are all in the vuex plug-in store, which is often used to store some data shared globally. But I still feel a little uncomfortable when using it, that is, I wrote a lot of repeated prefixes. At this time, these four objects appeared, which solved this pain point very well. mapState is mainly used to manipulate and obtain data, mapGetters is equivalent to computed properties, and mapMutations and mapActions are simplifications of mutation and action. They can be written as both objects and arrays.