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>

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

  • 相关阅读:
    AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
    Java中List集合去除重复数据的方法
    java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp区别和总结
    Spring 中配置log4j日志功能
    log4j配置文件加载方式
    程序中使用log4J打印信息的两种方式
    elasticsearch常用命令
    接私活必备的10个开源项目??
    初识Elasticsearch
    常用在线工具
  • 原文地址:https://www.cnblogs.com/ywang/p/5626122.html
Copyright © 2011-2022 走看看