• JS学习


    继承

     <script type="text/javascript">
        Father.prototype.name="wangxinngyu";
        function inherit(Target,Origin){
           function F(){}
           F.prototype=Origin.prototype;
           Target.prototype=new F();
           Target.prototype.construct=Target;//构造器
           Target.prototype.uber=Origin;//uber=super
        }
        function Father(){}
        function Son(){}
        inherit(Son,Father);
        var father=new Father();
        var son=new Son();
    </script>
    
    //过时的继承
        <script type="text/javascript"> 
        Father.prototype.name='wangxingyu';
        function Father(){
    
        }
        function Son(){
    
        }
        function inherit(Target,Origin){
        Target.prototype=Origin.prototype;  
        }
        inherit(Son,Father);
        var father=new Father();
        var son=new Son();
        </script>

    数组去重

        <script type="text/javascript">
        var arr=[11,2,2,2,2,2,2,23,3,3,35,];
        Array.prototype.unique=function (){
         var temp={};
         var arr=[];
         var len=this.length;
         for(var i=0;i<len;i++){
            if(!temp[this[i]]){
                temp[this[i]]="a";
                arr.push(this[i]);
            }
         }
         return arr;
        }
        console.log(arr.unique())
        </script>

    闭包

        <script type="text/javascript">
        var arr=[];
        function test(){
        for(var i=0;i<10;i++){
            (function (j){
                arr[j]=function (){
               document.write(j+" " );
            } 
            }(i))
            }
            return arr;    
        }
        var myArr=test();
        for(var j=0;j<10;j++){
            myArr[j]();
        }
        </script>

    立即执行函数

     立即执行函数的集中表达方式: 1.(function(){}()) W3C建议 2.(function(){})() 

    浅度克隆

        <script type="text/javascript">
        var obj={
            nmae:'wnag',
            age:18,
            sex:'male',
            array:['gd','dasd'],
            wife:{
                name:'wajdb',
                son:{
                    name:'das'
                }
            }
        }
        var obj1={}
        function clone(origin,target){
          for(var prop in origin){
              target[prop]=origin[prop]
          }
        }
        clone(obj,obj1);
        </script>

    深度克隆

        <script type="text/javascript">
      var obj={
            nmae:'wnag',
            age:18,
            sex:'male',
            array:['gd','dasd'],
            wife:{
                name:'wajdb',
                son:{
                    name:'das'
                }
            }
        }
        var obj1={}
        function deepClone(origin,target){
        // var target=target||{};
        tostr=Object.prototype.toString;
        arr="[object,Array]";
        for(var prop in origin){
            if(origin.hasOwnProperty(prop)){
              if(typeof(origin[prop])=='object'){
                //  if(tostr.call(origin[prop])==arr){
                //       target[prop]=[]; 
                //       }else{
                //        target[prop]={};
                //            }
                tostr.call(origin[prop])==arr ?target[prop]=[]: target[prop]={};
                 deepClone(origin[prop],target[prop]);     
              }
            else{
            target[prop]=origin[prop];
            }
        }
        }
        }
        deepClone(obj,obj1);
        </script>
  • 相关阅读:
    JavaScript 技巧
    网页打开客户端本机程序,未安装则提示要求安装
    IIS(World Wide Web Publishing Service)127 无法响应的解决方法
    Jquery hover事件 示例
    JavaScript MVC
    jquery调用基于.NET Framework 3.5的WebService返回JSON数据
    文件下载类
    网页打印局部示例
    未能执行URL(FCK)
    网页中各种宽高
  • 原文地址:https://www.cnblogs.com/wxy0715/p/12442273.html
Copyright © 2020-2023  润新知