zoukankan      html  css  js  c++  java
  • JavaScript ---Function

    关键字function用来定义函数。

    //函数声明式定义:
    function funcname([arg1[,args[...,argn]]]){
    statements
    }
    //函数表达式定义:
    var funcname = function ([arg1[,args[...,argn]]]){
    statements
    };

    注意,function语句里的花挂号是必需的,即使函数体只包含一条语句。

    在JavaScript中,函数是Function类的具体实例。而且都与其它引用类型一样具有属性和方法。

    函数名实际上是指向函数对象的指针,函数可以作为参数参与到传参和返回值中。

    函数的对象特性

    因为函数是Function的实例,而函数名仅仅是该实例的一个引用地址。因此可以作为参数和返回值参与到函数的传参过程中。

    function call_some_function(some_function, some_argument) {
        return some_function(some_argument);
    }
    function add_10(num) {
        return num + 10;
    }
    console.log(call_some_function(add_10,20)); //30
    

    函数的内部属性

    arguments | this

    • arguments对象中保存着传递给函数的参数
    • arguments.length返回传入参数的个数
    • Notelength属性表示函数定义时候默认接收的参数数量。arguments.length表示函数实际执行时接收的参数数量。
    function test_arguments() {
        if (arguments.length == 2) {
            console.log(arguments.length); 
            console.log(arguments); 
        } else {
            console.log(arguments.length); 
            console.log(arguments); 
            arguments.callee(4, 5);
        };
    }(1, 2, 3)
    /**
     3
    { '0': 1, '1': 2, '2': 3 }
    2
    { '0': 4, '1': 5 }
     **/
    
    • arguments.callee()主要用在递归函数中调用函数自身的情境中。js和别的语言不同在于函数名只是一个指针,可以随时变化,函数中利用函数名来调用自身属于高耦合,可能会出现问题,而arguments.callee()调用自身就会规避掉这个问题
    function factorial(num) {
        if (num <= 1) {
            return 1;
        } else {
            return num * factorial(num - 1);
        };
    }
    function callee_f(num) {
        if (num <= 1) {
            return 1;
        } else {
            return num * arguments.callee(num - 1);
        };
    }
    factorial(10); //运行正常
    f = factorial;
    factorial = null;
    f(10); //error
    
    callee_f(10); //运行正常
    f = callee_f;
    callee_f = null;
    f(10); //运行正常
    
    • this主要用来帮助函数引用函数所处作用域中的对象。
    var color = 'red';
    function syaColor() {
        console.log(this.color);
    }
    syaColor(); //red
    var o = new Object();
    o.color = 'blue';
    o.sayColor = sayColor;
    o.sayColor(); //blue
    

    call()和apply()

    call()和apply()是每个函数都包含的自有方法。之前已经提到了函数是定义的对象,那么调用函数时候,函数中的this是对当前与下变量的调用。而如果想改变函数执行所在域空间,则可以使用call()和apply()来实现。

    color = 'red';
    var o = {color: 'blue'};
    function sayColor() {
        console.log(this.color);
    }
    sayColor(); //red
    sayColor.call(this); //red
    sayColor.call(o); //blue
    

    app()和call()的作用是相同的,区别主要在于传入参数的不同。

    call(this,para1,prar2,prar3) 第一个参数是函数要执行的作用域,后面的参数是函数的输入参数,有多少个依次写多少个。

    apply(this,[para1,para2,prara3])第一个参数也是函数要执行的作用域,后面是一个Array的数组对象。

    使用call()/apply()来扩充作用域最大的好处是对象和方法的解耦。

    内置对象

    Global对象可以理解成最外层的对象,所有的对象,以及不属于其它对象的属性和方法都被包含在Global对象中。
    isNaN(x) 用来检查参数x是否为数字。如果为数字返回false,否则返回true
    isFinite(x) 用来检查参数x是否为无穷大/小,如果是无穷大/小,则返回true
    parseInt(x) 用来解析字符串并返回整数
    parseFloat(x) 用来解析字符串并返回浮点数
    encodeURI()encodeURIComponent()会对字符串进行特殊的UTF-8编码,规避一些特殊字符来让浏览器能够读懂。他俩的区别主要在于encodeURI()不会对本身属于URI的特殊字符进行编码,而encodeURIComponent()会对其发现的所有非标准字符进行编码。

    var uri = "http://www.wrox.com/illegal value.htm#start";
    //http://www.wrox.com/illegal%20value.htm#start
    console.log(encodeURI(uri))
    //http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start
    console.log(encodeURIComponent(uri))
    
    • 对应的解码函数为decodeURI()decodeURIComponent()
    • eval(script) 用来将script的内容在解释器中执行并返回对应的结果。非常强大!

    Note:在浏览器中,windows对象封装了Global对象,并承担了很多额外的任务和功能。

    Math对象为另一个内置对象。为JavaScript提供了数学计算功能。

  • 相关阅读:
    Qt中使用cout, cin, cerr
    linux下清理系统垃圾
    linux清理内存命令
    boost 特点
    linux boost 安装
    valgrind 的使用及错误信息分析
    ArcGIS Engine 编辑介绍
    ArcGIS Engine 编辑- IWorkspaceEdit
    ArcGIS Engine 编辑- ITask
    CreateFeature与CreateFeatureBuffer区别
  • 原文地址:https://www.cnblogs.com/zhxhdean/p/4301153.html
Copyright © 2011-2022 走看看