javaScript basics summary (XI)

1, and partial function Currie

  Partial function

  If a function takes two arguments, the first argument and then when we call is established, the door then I can create a partial function passed as a parameter to simplify the problem

  Examples

  function mul(a,b){

    return a * b;

  }

  let double = mul.bind(null,2);

  alert(double(3));//=mul(2,3) =6

  let triple = mul.bind(null,3); 

  alert(triple(3));// = mull(3,3)=9

  No Context using partial function

 1 function partial(func, ...argsBound) {
 2   return function(...args) { // (*)
 3     return func.call(this, ...argsBound, ...args);
 4   }
 5 }
 6 
 7 // 用法:
 8 let user = {
 9   firstName: "John",
10   say(time, phrase) {
11     alert(`[${time}] ${this.firstName}: ${phrase}!`);
12   }
13 };
14 
15 // Add a partial function method, this function can now say as a function of the first 
16 user.sayNow = partial (user.say, new new a Date () getHours () +. ' : ' + New new . A Date () getMinutes () );
 17  
18 user.sayNow ( " the Hello " );
 19  // result like this:
 20  // ! [10:00] John: the Hello 
21  
22  
23  partial (FUNC [, arg1, arg2 ...]) call the result is a wrapper function func based, as well as:
 24 and its function consistent passed the this (user.sayNow call is for User)
 25 is then passed ... argsBound - the parameters passed from the calling partial function ( " 10:00" )
 26 is then passed ... args - The arguments passed in the package (the Hello)

  Currying

  Personal understanding: nest multiple functions

function curry(func) {
  return function(a) {
    return function(b) {
      return func(a, b);
    };
  };
}

// 用法
function sum(a, b) {
  return a + b;
}

let carriedSum = curry(sum);

alert( carriedSum(1)(2) ); // 3

  Currying is to f(a,b,c)be in a f(a)(b)(c)converted form is called. JavaScript implementation version is usually reserved for the normal function call and return partial function of these two characteristics in case of an insufficient number of parameters

2, the function arrow

  Arrow feature is not "this"

  Arrow function does this. If you visit this, from externally acquired  

 1 let group = {
 2   title: "Our Group",
 3   students: ["John", "Pete", "Alice"],
 4 
 5   showList() {
 6     this.students.forEach(
 7       student => alert(this.title + ': ' + student)
 8     );
 9   }
10 };
11 
12 group.showList();

  But foreach inside, if encapsulates a function, you will lose this

 1 let group = {
 2   title: "Our Group",
 3   students: ["John", "Pete", "Alice"],
 4 
 5   showList() {
 6     this.students.forEach(function(student) {
 7       // Error: Cannot read property 'title' of undefined
 8       alert(this.title + ': ' + student)
 9     });
10   }
11 };
12 
13 group.showList();

   Function has no arrow "arguments" (parameter)

  Arrow function

 1 function defer(f, ms) {
 2   return function() {
 3     setTimeout(() => f.apply(this, arguments), ms)
 4   };
 5 }
 6 
 7 function sayHi(who) {
 8   alert('Hello, ' + who);
 9 }
10 
11 let sayHiDeferred = defer(sayHi, 2000);
12 sayHiDeferred("John"); // 2 秒后打印 Hello, John

  No arrow function

1 function defer(f, ms) {
2   return function(...args) {
3     let ctx = this;
4     setTimeout(function() {
5       return f.apply(ctx, args);
6     }, ms);
7   };
8 }

 

Guess you like

Origin www.cnblogs.com/xiaoqiyaozou/p/11490990.html