zoukankan      html  css  js  c++  java
  • js 面向对象方法初整理

     <script>
         function f1(){
             alert(1);
         }
     
         function f2(){
             alert(2);
         }
         var func = "";
         function f(id){
             if(id%2 == 0){
                 func = f1;//将F1指针指向对应的函数
             }else{
                 func = f2;
             }
             func();//调用相应的方法
         }
         f(0);
     </script>
    <script>
        var anim = function(){}
        anim.start = function(){//定义类属性
            alert("start");
        }
        调用方法:anim.start();
        anim.prototype.stop = function(){//对象属性
            alert("stop");
        }
        调用方法:var aa = new anim();
                    aa.stop();        
    </script>    
    <input type="text" value="dfas" id="input1" />
    <span id="hello">hello myworld</span>
    <script>
        $.extend({//定义jquery的扩展方法(将该方法合并到jquery的全局对象中去)
            sayHello:function(){
                alert("hello world!");
            },
            min: function(a, b) { 
                return a < b ? a : b; 
            },
            max: function(a, b) {
                return a > b ? a : b; 
            }
        })
        调用方法:var hello = $.sayHello();
                     alert(jQuery.min(2,3));  //2
                     alert(jQuery.max(4,3));  //4
    var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; $.extend(settings, options);//用一个或多个其他对象(options对象)来扩展一个对象(settings对象),返回被扩展的对象 console.log(settings); $.fn.extend({ //对jQuery.prototype进行扩展 (方法1) alertWhileClick:function() { $(this).click(function(){ alert($(this).val()); }); } }); $("#input1").alertWhileClick();
    $.prototype.sayHellow
    = function(){ //对jQuery.prototype进行扩展 (方法2) $(this).click(function(){ alert("hello myworld"); }) } $("#hello").sayHellow();

      String.prototype.trim = function() {  //清除两边空格
          return this.replace(/(^s*)|(s*$)/g, '');  
      }; 
      var str = “ hello world ”;
      
    str = str.trim();
      console.log(str);
    </script>

    如果本文有不对的地方,请指教

  • 相关阅读:
    QT实现软件重启
    Qt 延时
    gcc 创建库及使用
    verilog 奇数分频设计
    内核中的 likely() 与 unlikely()
    TFT LCD 参数详解
    手动安装m4, autoconf, automake, libtool
    [其他] 蒙特卡洛(Monte Carlo)模拟手把手教基于EXCEL与Crystal Ball的蒙特卡洛成本模拟过程实例:
    Origin9.1如何绘制风向玫瑰图(Binned Data)?
    Origin9.1如何使用原始数据(Raw Data)绘制风向玫瑰图
  • 原文地址:https://www.cnblogs.com/ywang/p/5626122.html
Copyright © 2011-2022 走看看