Java Functional Programming - Handwritten Curry Function - Composite Function - Strict Mode

1. Realization of curried function (self-implementation) (interview questions)

// 柯里化函数的实现hhCurrying
function hhCurrying(fn) {
    
    
  function curried(...args) {
    
    
    /*判断当前已经接收的参数的个数, 和参数本身需要接受的参数
    是否已经一致了*/
    // 1.当已经传入的参数 大于等于 需要的参数时, 就执行函数
	//args.length:当前已经接收的参数个数,
 	//fn.length函数本身需要接收的参数的个数
    if (args.length >= fn.length) {
    
    
      // fn.call(this, ...args)
      return fn.apply(this, args)
    } else {
    
    
      // 没有达到个数时, 需要返回一个新的函数, 继续来接收的参数
      function curried2(...args2) {
    
    
        /* 接收到参数后, 需要递归调用curried来检查函数的参数
        是否达到一致*/
        //concat函数:连接两个数组
        return curried.apply(this, args.concat(args2))
      }
      return curried2
    }
  }
  return curried
}
//测试1
function add1(x, y, z) {
    
    
  return x + y + z
}

//转为柯里化函数
var curryAdd = hhCurrying(add1)

console.log(curryAdd(10, 20, 30))   //60
console.log(curryAdd(10, 20)(30))  //60
console.log(curryAdd(10)(20)(30))   //60

//测试2
function foo(x, y, z) {
    
    
  return x + y + z;
}

var result1 = foo.call({
    
    }, 1, 2, 3);
console.log(result1); //6

var curryFoo = hhCurrying(foo);
var result2 = curryFoo.call({
    
    }, 1)(2)(4);
console.log(result2); //7

2. Understanding of combination functions

  • (1) Now it is necessary to call a function for a certain data , execute two functions fn1 and fn2, these two functions are executed in sequence, then if we need to call two functions every time, the operation will appear repeat.
  • Solution: Combine these two functions and call them automatically in sequence,
  • This process is the combination of functions, which we call Compose Function.
//未组合:
function double(num) {
    
    
  return num * 2;
}

function square(num) {
    
    
  return num ** 2;
}

var count = 10;
var result = square(double(count));
console.log(result); //400

// 实现最简单的组合函数
function composeFn(m, n) {
    
    
  return function (count) {
    
    
    return n(m(count));
  };
}

var newFn = composeFn(double, square);
console.log(newFn(10));  //400

3. Implementation of general combination function (self-implementation)

function hhCompose(...fns) {
    
    
  var length = fns.length
  //传入的参数必须是一个函数,否则抛出错误
  for (var i = 0; i < length; i++) {
    
    
    if (typeof fns[i] !== 'function') {
    
    
      throw new TypeError("Expected arguments are functions")
    }
  }

  function compose(...args) {
    
    
    var index = 0
    // 判读一下是否有有值(有无长度),有值的情况执行一下第一个函数
    var result = length ? fns[index].apply(this, args): args
    while(++index < length) {
    
    
      result = fns[index].call(this, result)
    }
    return result
  }
  return compose
}
//测试
function double(m) {
    
    
  return m * 2;
}

function square(n) {
    
    
  return n ** 2;
}

var newFn = hhCompose(double, square);
console.log(newFn(10));  //400

4. JS additional knowledge supplement - with statement - eval function - strict mode

4.1, with statement (not recommended) (with statement is not allowed in strict mode)

Function: the with statement extends the scope chain of a statement (can form its own scope chain)

var message = "Hello World";
var obj = {
    
     name: "why", age: 18, message: "obj message" };
// with语句: 可以形成自己的作用域
function foo() {
    
    
  function bar() {
    
    
    with (obj) {
    
    
      console.log(message); //obj message
      console.log("------");
    }
  }
  bar();
}
foo();

var info = {
    
     name: "kobe" };
with (info) {
    
    
  console.log(name); //kobe
}

4.2, eval function (global function)

  • (1) Function: eval is a special function that can run the incoming string as JavaScript code.
  • (2) It is not recommended to use eval in development:
    ①The readability of eval code is very poor (code readability is an important principle of high-quality code)
    ②eval is a string, so it may be deliberately tampering, it may cause the risk of being attacked;
    ③The execution of eval must pass through the JS interpreter and cannot be optimized by the JS engine;
var jsString = 'var message = "Hello World"; console.log(message);';
eval(jsString); //Hello World

4.3, strict mode (''use strict'';)

  • strict mode restrictions
  • (1) Cannot accidentally create global variables
  • (2) Make the assignment operation that causes silent failure (silently fail, note: no error and no effect) throw an exception
  • (3) Try to delete undeletable attributes in strict mode
  • (4) Function parameters are not allowed to have the same name
  • (5) Octal syntax that does not allow 0
  • (6) The use of with is not allowed
  • (7) eval no longer references variables for the upper layer
  • (8) this binding will not be converted to an object by default
 "use strict";

// 1. 禁止意外创建全局变量
 message = "Hello World"  
//会报message is not defined的错误,message未声明  
 console.log(message)

 function foo() {
    
    
   age = 20   //会报age is not defined的错误,age未声明
 }

 foo()
 console.log(age) 
 "use strict";
// 2.不允许函数有相同的参数名称
function foo(x, y, x) {
    
    
  console.log(x, y, x)     
  //会报Duplicate parameter name not allowed in this context的错误,
  //在此上下文中不允许重复的参数名称
 }
foo(10, 20, 30)
// 3.静默错误/试图删除不可删除的属性
 "use strict";
true.name = "abc"; 
//Cannot create property 'name' on boolean 'true'
NaN = 123;  
// Cannot assign to read only property 'NaN' of object '#<Object>'
var obj = {
    
    };
Object.defineProperty(obj, "name", {
    
    
  configurable: false,
  writable: false,
  value: "fifih",
});
console.log(obj.name);  //fifih
// obj.name = "kobe"; 
//Cannot assign to read only property 'name' of object '#<Object>'
delete obj.name;
// Cannot delete property 'name' of #<Object>
 "use strict";
// 4.不允许使用原先的八进制格式 0123 改成0O123就可以
 var num = 0o123 // 八进制
 var num2 = 0x123 // 十六进制
 var num3 = 0b100 // 二进制
 console.log(num, num2, num3)  //83 294 4
 "use strict";
 // 5.with语句不允许使用
// 6.eval函数不会向上引用变量了
message = "Hello eval"  
var jsString = 
  '"use strict"; var message = "Hello World"; console.log(message);'
eval(jsString)   //Hello World
console.log(message)  //报错 message is not defined

Guess you like

Origin blog.csdn.net/weixin_53737663/article/details/127486766