Last active
December 20, 2015 04:29
-
-
Save alanerzhao/6071241 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//原型链继承 | |
//examples extend(TwoShape,Shape) | |
function extend (Child, Parent) { | |
//临时构造函数 | |
var F = function() {}; | |
F.prototype = Parent.prototype; | |
Child.prototype = new F(); | |
Child.prototype.constructor = Child; | |
Child.uber = Parent.prototype; | |
} | |
//浅拷贝(引用类型受影响 ) | |
//examples shallowCopy(TwoShape,Shape) | |
function shallowCopy (Child, Parent) { | |
var P = Parent.prototype, | |
C = Child.prototype, | |
i; | |
//遍历对象拷贝 | |
for(i in P) { | |
C[i] = P[i] | |
} | |
C.uber = P; | |
} | |
//Firebug Object Extend | |
//examples var hero = extendCopy(person); | |
function extendCopy(p) { | |
var c = {}; | |
for (var i in p) { | |
c[i] = p[i]; | |
} | |
c.uber = p; | |
//返回对象 | |
return c; | |
} | |
//深拷贝 解决引用类型的问题 | |
//examples var mydeep = deepCopy(parent); | |
function deepCopy(p ,c) { | |
var c = c || {}, | |
i; | |
for(i in p) { | |
//检查拷贝对象是不是引用类型 | |
if(typeof p[i] === 'object') { | |
//检查是不是引用数组 | |
c[i] = (p[i].constructor === Array) ? [] : {}; | |
deepCopy(p[i],c[i]); | |
} else { | |
c[i] = p[i]; | |
} | |
} | |
return c; | |
} | |
//object() Douglas Crockford 原型继承 | |
//examples var mydeep = object(parent); | |
function object(o) { | |
var n; | |
function F() {} | |
F.prototype = o; | |
n = new F(); | |
n.uber = o; | |
return n; | |
} | |
//原型继承与属性拷贝混合 | |
function objectPlus(o, stuff) { | |
var n; | |
function F() {} | |
F.prototype = o; | |
n = new F(); | |
n.uber = o; | |
for(var i in stuff) { | |
n[i] = stuff[i]; | |
} | |
return n; | |
} | |
var shape = { | |
name : "shape", | |
tosay : function (){ return this.name;} | |
} | |
var twoDee = objectPlus(shape,{ | |
name:"2b", | |
tosay: function (){ return this.name + "2"} | |
}) | |
var triangle = objectPlus(twoDee,{ | |
name: "triangle", | |
side:0, | |
height:0, | |
tosay: function () {return this.side + this.height} | |
}) | |
var my = objectPlus(triangle,{ | |
side:4, | |
height:4 | |
}) | |
console.log(my); | |
//多重继承 | |
function multi (){ | |
var n = {}, | |
stuff, | |
j=0, | |
len = arguments.length; | |
for(j;j < len;j++){ | |
stuff = arguments[j]; | |
//遍历对象属性方法 | |
for(var i in stuff) { | |
n[i] = stuff[i]; | |
} | |
} | |
return n; | |
} | |
var one = { | |
name : "shape", | |
toHelp : function (){ | |
return this.name; | |
} | |
} | |
var two = { | |
name : "two", | |
toHelp: function (){ | |
return this.name; | |
} | |
} | |
var four = multi(shape,TwoDee,{ | |
name : "four", | |
toHelp: function() { | |
return this.side + this.height; | |
}, | |
side : 0, | |
height: 2 | |
}) | |
console.log(four.toHelp()); | |
//寄生式模式 | |
var twoD = { | |
name : "2d Shape", | |
dimensions: 2 | |
} | |
function triangle(s,h){ | |
var that = object(twoD); | |
that.name = "Triangle"; | |
that.getArea = function () { | |
return this.side * this.height / 2 | |
} | |
that.side = 8; | |
that.height = h; | |
return that; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment