闭包

function createComparisonFunction(propertyName) {
    return function (object1, object2) {
        var value1 = object1[propertyName];
        var value2 = object2[propertyName];

        if (value1 < value2) {
            return -1;
        } else if (value1 > value2) {
            return 1;
        } else {
            return 0;
        }
    };
}
//创建函数
var compare = createComparisonFunction("name");
//调用函数
var result = compare({ name: "Jim" }, { name: "Jack" });
//解除对匿名函数的引用,以便释放内存
compare = null;
View Code

相关文章:

  • 2022-02-09
  • 2021-12-31
  • 2021-11-28
  • 2021-07-08
  • 2021-08-12
  • 2022-01-10
  • 2022-02-04
猜你喜欢
  • 2022-02-17
  • 2021-06-10
  • 2021-12-30
  • 2021-12-02
  • 2021-07-20
  • 2022-01-20
  • 2021-11-20
  • 2022-01-03
相关资源
相似解决方案