JavaScript Javascript OOP
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript Javascript OOP相关的知识,希望对你有一定的参考价值。
var Klass = function(prop) {
var klass = function(){ if(this.init) this.init.apply(this, arguments);};
klass.prototype = prop || {};
klass.constructor = Klass;
klass.extend = Klass.extend;
return klass;
};
Klass.extend = function(prop){
function setParent(fn, parent) {
return function() {
this.parent = parent;
return fn.apply(this, arguments);
}
}
function merge(prev, next){
for (var name in next){
if (prev[name] && (typeof prev[name] == 'object' && typeof next[name] == 'object'))
merge(prev[name], next[name]);
else
prev[name] = next[name];
}
return prev;
};
for (var name in this.prototype) {
if (!prop[name])
prop[name] = this.prototype[name];
else if(typeof prop[name] == 'function' && typeof this.prototype[name] == 'function')
prop[name] = setParent(prop[name], this.prototype[name]);
else if(typeof prop[name] == 'object' && typeof this.prototype[name] == 'object')
prop[name] = merge(prop[name], this.prototype[name]);
}
return new Klass(prop);
};
//Modo de uso
var Hobbit = Klass({
bailar: function(){
.....
}
});
var Frodo = Hobbit.extend({
anillo: true;
desaparecer: function(){
if (this.anillo)
this.display("none");
}
});
以上是关于JavaScript Javascript OOP的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript Javascript OOP表格验证器
JavaScript之基础-15 JavaScript OOP(概述对象模板)
JavaScript OOP Javascript模板与构造函数和继承
JavaScript对象及初识OOP
JavaScript的面向对象编程(OOP)——类
JavaScript 与 OOP