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>

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

posted @ 2016-06-29 11:04  小时光  阅读(201)  评论(0编辑  收藏  举报