zoukankan      html  css  js  c++  java
  • 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次 日志
    
    

  • 相关阅读:
    响应式设计工作总结
    首页性能指标
    前端工作小结
    html5、css3小应用
    循环滚动实例
    程序员修炼之道笔记(转)
    一个操作系统的实现(1)
    scrum role
    一个操作系统的实现(3)
    awk&sed story
  • 原文地址:https://www.cnblogs.com/bkylee/p/6884073.html
Copyright © 2011-2022 走看看