每天CookBook之JavaScript-032

  • 将函数作为参数使用
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>032</title>
</head>
<body>
    
</body>
<script type="text/javascript">
(function () {
    function otherFunction (x,y,z) {
         x(y,z);
    }

    var param = function(arg1, arg2) {
        console.log(arg1 + " " + arg2);
    }

    otherFunction(param, 'hello', 'world');
})(); 

(function () {
    var param = function inner () {
         return typeof(inner);
    }
    console.log(param());
})(); 

(function () {
    var nums = [1,45,2,16,9,12];
    var sum = 0;
    for (var i = 0; i < nums.length; i++) {
        sum += nums[i];
    }
    console.log(sum);
})(); 
</script>
</html>
posted @ 2016-07-14 20:37  4Thing  阅读(112)  评论(0编辑  收藏  举报