标签:
在原型模式中this常用的有两种情况:
//this.xxx=xxx;当前类的实例;
//某一个方法中的this,看谁执行的this是谁
function Fn (){ this.x = 100; this.y =200; this.getX = function() { console.log(this.x) }; } Fn.prototype ={ constructor : Fn, y:300, getX : function(){ console.log(this.x) } getY : function(){ console.log(this.y) } }; var f = new Fn; f.getX();->100 f.prototype.getX();->undefined
给内置类的原型扩展方法
Array.prototype.myUnique = function(){ var obj = {}; for(var i= 0;i<this.length;i++){ var cur = this[i]; if(obj[cur]===cur){ this[i] = this[this.length-1]; this.length--; i--; continue; } obj[cur] = cur; } obj = null; return this;//实现链式写法 } var ary = [.......]; ary.myUnique().sort(function(a,b){return a-b});
标签:
原文地址:http://www.cnblogs.com/han6054/p/5346737.html