javascript 闭包(CLOSURES)详解
1月 8, 2018 |
Nix.Huang
如下代码,摘录自javascript高级编程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
window.test = "javacoder.cn" function createComparisonFunction(propertyName, param1) { var xyz="welcome"; return function(object1, object2) { var value1 = object1[propertyName]; var value2 = object2[propertyName]; var xyz_copy=xyz; if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } }; } //create function var compareNames = createComparisonFunction("name", "param1"); //call function var result = compareNames({ name : "Nicholas" }, { name : "Greg" }); //dereference function - memory can now be reclaimed compareNames = null; |