Last active
May 8, 2016 10:15
-
-
Save zjhiphop/7356657 to your computer and use it in GitHub Desktop.
Different type of JS Extend
This file contains 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
//Simple Prototype extend | |
var extend = (function () { | |
var f = function(){}; | |
return function (c, p) { | |
f.prototype = p.prototype; | |
c.prototype = new f; | |
c._super = p.prototype; | |
c.prototype.constructor = c; | |
} | |
})(); | |
//Simple Class Extend | |
var Create = Object.Create || (function (){ | |
var f = function(){}; | |
return function(p){ | |
f.prototype = new p; | |
return new f; | |
} | |
})(); | |
//use __proto__ | |
var New = function(F) { | |
var n = {"__proto__": F.prototype}; | |
return function (){ | |
F.apply(n, arguments); | |
return n; | |
} | |
} | |
var Create = function (P){ | |
return {"__proto__": P}; | |
} | |
//Syntax suger used to simulate 'Class' | |
/* | |
1. _init as constructor | |
2. Store static property of super class | |
3. Child class can access super Class | |
*/ | |
var Klass = function (Parent, props){ | |
var Child, F, i; | |
//1. Create new constructor; | |
Child = function (){ | |
if(Child._super && Child._super.hasOwnProperty("_init")){ | |
Child._super._init.apply(this, arguments); | |
} | |
if(Child.hasOwnproperty("_init")){ | |
Child._init.apply(this, arguments); | |
} | |
} | |
//2. inherit | |
Parent = Parent || Object; | |
f = function() {}; | |
f.prototype = Parent.prototype; | |
Child.prototype = new f; | |
Child.prototype.constructor = Child; | |
Child._super = Parent.prototype; | |
//3. add static property | |
for(i in props) { | |
Child.prototype[i] = props[i]; | |
} | |
return Child; | |
} | |
//Coping properties | |
var clone = function (P, C, isDeep){ | |
var i; | |
var toStr = Object.prototype.toString; | |
var OBJ_ARRAY_STR = "[Object Array]"; | |
C = C || {}; | |
for (i in P) if(P.hasOwnProperty) { | |
if(isDeep && typeof P[i] === "Object") { | |
C[i] = (toStr.call(P[i])) === OBJ_ARRAY_STR ? [] : {}; | |
clone(P[i], C[i], true); | |
} else { | |
C[i] = parent[i] | |
} | |
} | |
} | |
//Mix-in | |
var Mix = function() { | |
var arg; | |
var len = arguments.length; | |
var child = {}; | |
var prop; | |
for(arg = 0 ; arg < len; arg++) { | |
for(prop in arguments[arg]) if(arguments[arg].hasOwnProperty(prop)){ | |
child[prop] = arguments[arg][prop]; | |
} | |
} | |
return child; | |
} | |
// Proxy | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy | |
function extend(sup,base) { | |
var descriptor = Object.getOwnPropertyDescriptor( | |
base.prototype,"constructor" | |
); | |
base.prototype = Object.create(sup.prototype); | |
var handler = { | |
construct: function(target, args) { | |
var obj = Object.create(base.prototype); | |
this.apply(target,obj,args); | |
return obj; | |
}, | |
apply: function(target, that, args) { | |
sup.apply(that,args); | |
base.apply(that,args); | |
} | |
}; | |
var proxy = new Proxy(base,handler); | |
descriptor.value = proxy; | |
Object.defineProperty(base.prototype, "constructor", descriptor); | |
return proxy; | |
} | |
var Person = function(name){ | |
this.name = name; | |
}; | |
var Boy = extend(Person, function(name, age) { | |
this.age = age; | |
}); | |
Boy.prototype.sex = "M"; | |
var Peter = new Boy("Peter", 13); | |
console.log(Peter.sex); // "M" | |
console.log(Peter.name); // "Peter" | |
console.log(Peter.age); // 13 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment