我知道,用string.propotype可以对字符串写扩展属性或者方法,,
那我想请问如何对任意变量构架扩展方法或属性尼??

解决方案 »

  1.   

    string.propotype
    可以给string扩展一个左右去掉空格的方法,trim。
    百度搜一下就知道了。对象都可以用prototype扩展
    如:
    <script>
    function Person(name){
    this.name = name;
    }

    Person.prototype.getName = function(){
    alert(this.name);
    };

    var p = new Person('11');
    p.getName();</script>
      

  2.   

    我不是需要一个固定对象的扩展方法,而是所有的。如function (startStr, endStr,boolen) {}我要为里面所有参数定义个扩展方法。以后换了一个function,换了参数,我的扩展方法依然可以用,不是针对某个对象
      

  3.   


    Object.prototype.方法名 = function(){};
    Function.prototype.方法名 = function(){};
    等等这些都可以扩展。
    你新声明的function a(){};
    就有了一个你Function.prototype出来的新方法