javascript中的高阶函数, 和 类定义Function, 和apply的使用

参考: http://www.cnblogs.com/delin/archive/2010/06/17/1759695.html

js中的类, 也是用function关键字来定义的:

function Person(name, age){
    this.name=name;
    this.age=age;
    this.sayHello= function(){alert 'say hello!';}
}

js 的类, 属性和方法都是用 this.property| functionName: 方法不要加括号.
类一般用首字母 大写的方式:

apply: 应用, 适用, 套用; 申请; 涂抹, 傅.
在一个函数里面, 或一个类里面, 使用 OtherClass.apply(this, arguments)的意思是: 在函数(或类)里面, "apply应用 类OtherClass"的属性和方法...


高阶函数的用法.

javascript 在一个函数中, 函数的参数可以是整数, 字符串, 还可以是 函数名. 这样的函数就叫做 "高阶函数"

function output(count, message){
    for(i=0; i<count; i++){
        alert(message);
    }
}

function log(count, message){
    for(i=0; i<count; i++){
        console.log(message);
    }
}

function advanced_func (count, message, action){
    for (i=0;i<count; i++){
        action(message);
    }

}

advanced_func就是一个高阶函数, 它的参数action 就是一个 函数名, 那么, 调用advanced时,就可以 做不同的动作了:

如:
advanced_func(5, "hello world", "output");  // 这个调用 输出 5个message
advanced_func(5, "hello world", "log");  // 这个调用 做 5次 日志

posted @ 2017-05-21 10:11  noitanym  阅读(208)  评论(0编辑  收藏  举报