zoukankan      html  css  js  c++  java
  • JavaScript函数

    JavaScript函数实际上是个对象。每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法。由于函数是对象,因此函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定(函数名是指针就可以随便指啦)。引自《JavaScript高级程序设计》。

    既然函数是对象,就可以给他设置属性,相当于函数内部的静态变量。

    //Though my English is not very well, my Chinese English is not so bad.
    function foo(){
        if(typeof foo['aaa'] == 'undefined'){
            //caculate value of aaa
            foo['aaa'] = 'value of aaa';
        }
        //you can use or change the value of aaa,
        //the value of aaa will not be change in your next invocation of foo
        foo['aaa'] = 'change value of aaa';
        alert(foo['aaa']);
    }
    foo();
    

    接下来看另外一个函数,这个函数是对象定义函数,该函数分别在内部和外部定义了一个静态变量,这里定义的只是一个常规的String类型的变量,如果是Function类型的变量,那就是静态函数了。

    function Foo(){
        //define a static variable in the object define function
        if(typeof Foo.aaa == 'undefined'){
            //caculate value of aaa
            Foo['aaa'] = 'value of aaa';
        }
        //you can use or change the value of aaa,
        //the value of aaa will not be change in your next invocation of foo
        Foo.aaa = 'change value of aaa';
        alert(Foo['aaa']);
        
        this.name = 'bbb';
    }
    //define a static variable out of the object define function
    Foo.age = '18';
    var foo = new Foo();
    //if you use foo.aaa or foo.age will be error
    alert(foo.name + '---' + Foo.aaa+ '---' + Foo.age);
    

      

  • 相关阅读:
    Android获取View对应的Bitmap
    Android按需添加Google Play服务
    Android自定义View的构造函数
    两个Fragment之间如何传递数据
    SmartImageView
    onSingleTapUp()和onSingleTapConfirmed()的区别
    Android Fragment add/replace以及backstack
    InputStream与InputStreamReader的区别
    Android手动签名
    使用后台服务数据更新UI
  • 原文地址:https://www.cnblogs.com/realwall/p/2214408.html
Copyright © 2011-2022 走看看