继承
<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>