// Parent Class
var jQuery = 'jQuery';
var MyClass = (function($){
// Init
function init(a, b) {
this.a = a;
this.b = b;
return this;
}
// Private
var privateVariable = 'c';
function privateMethod () {
return 'd';
}
// Public
var publicVariable = $;
function publicFunction () {
return privateVariable + privateMethod();
}
// Static (Class) property and method
init.classVariable = 0;
init.classFunction = function () {
init.classVariable++;
return this.classVariable;
}
// Make Public
init.prototype = {
publicVariable: publicVariable,
publicFunction: publicFunction
};
return init;
})(jQuery);
var Obj = new MyClass('a', 'b');
console.log(MyClass.classFunction());
for(var key in Obj) {
console.log('Key: ' + key + ' | Value: ' + Obj[key]);
}
// Child Class extends Parent Class
function extend(Parent, Child){
function Temp () {}
Temp.prototype = Parent;
for (var property in Child) {
Temp.prototype[property] = Child[property];
}
for (var property in Child.prototype) {
Temp.prototype[property] = Child.prototype[property];
}
var ParentObject = function () {}
ParentObject.prototype = new Parent();
for (var property in ParentObject) {
Temp.prototype[property] = ParentObject[property];
}
for (var property in ParentObject.prototype) {
Temp.prototype[property] = ParentObject.prototype[property];
}
var ResultChild = Child;
ResultChild.prototype = Temp.prototype;
return ResultChild;
}
var Underscore = 'Underscore'
var ChildClass = extend(MyClass, (function(_){
// Init
function init(g, h) {
this.g = g;
this.h = h;
return this;
}
// Private
var privateVariable2 = 'k';
function privateMethod2 () {
return 'l';
}
// Public
var publicVariable2 = _;
function publicFunction2 () {
return privateVariable2 + privateMethod2();
}
// Static (Class) property and method
init.classVariable2 = 1;
init.classFunction2 = function () {
init.classVaribale2 += 1;
return this.classVariable2;
}
// Make Public
init.prototype = {
publicVariable2: publicVariable2,
publicFunction2: publicFunction2
};
return init;
})(Underscore));
var ChildObj = new ChildClass('g', 'h');
console.log(ChildClass.classFunction2());
for(var key in ChildObj) {
console.log('Key: ' + key + ' | Value: ' + ChildObj[key]);
}
Комментариев нет:
Отправить комментарий