闭包

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>闭包</title>
</head>
<body>
 

<script>
//闭包是一种现象,在一个函数内部访问的外部的变量就产生了闭包
//在creatCompareFunction函数内部创建了另一个匿名函数。
function creatCompareFunction (propertyName) {
return function(object1,object2) {
//在这个函数中可以访问creatCompareFunction函数作用域中的propertyName变量
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
};
}
 
</script>
</body>
</html>
posted @ 2019-05-26 23:17  没有翅膀的脚  阅读(87)  评论(0编辑  收藏  举报