日期:2014-05-16 浏览次数:20436 次
function MyA(){
if(MyA.prototype.baseClass!==undefined){
MyA.prototype.baseClass.call(this);
}
……一般代码
}
function MyB(){
if(MyB.prototype.baseClass!==undefined){
MyB.prototype.baseClass.call(this);
}
……一般代码
}
MyB.prototype=new MyA();
MyB.prototype.baseClass=MyB.prototype.constructor;//使用baseClass引用//基类的构造器函数。
MyB.prototype.constructor=MyB;//恢复子类的构造器属性 用于未来通过这个属性判//断对象的类型。
var myA=new MyA();
var myB=new MyB(); if(ClassName.prototype.baseClass!==undefined){
ClassName.prototype.baseClass.call(this);
} MyA.prototype=new Base(); MyA.prototype.baseClass= MyA.prototype.constructor;//使用baseClass引用//基类的构造器函数。 MyA.prototype.constructor= MyA;//恢复子类的构造器属性 用于未来通过这个属性判//断对象的类型。
1,copyClassPrototypeProperties复制类的原型属性
/**
* 把源类原型上的所有属性复制到目标对象上,第一个参数是boolean值,表示是否覆盖目标类的原型的属性
*/
function copyClassPrototypeProperties(isOverride,receivingClass,givingClass){
if(arguments.length>3){
for(var i=3;i<arguments.length;i++){
receivingClass.prototype[arguments[i]]=givingClass.prototype[arguments[i]];
}
}else if(arguments.length==3){
for(var name in givingClass.prototype){
if(isOverride){
receivingClass.prototype[name]=givingClass.prototype[name];
}else{
//不覆盖
if(!receivingClass.prototype[name]){
receivingClass.prototype[name]=givingClass.prototype[name];
}
}
}
}else{
throw "Please give 3 arguments at least!";
}
}
2,copyObjectProperties复制对象的属性
/*
* 把源对象的所有属性复制到目标对象上,第一个参数是boolean值,表示是否覆盖目标对象的属性
*/
function copyObjectProperties(isOverride,receivingObject,givingObject){
if(arguments.length>3){
for(var i=3;i<arguments.length;i++){
receivingObject[arguments[i]]=givingObject[arguments[i]];
}
}else if(arguments.length==3){
for(var name in givingObject){
if(isOverride){
receivingObject[name]=givingObject[name];
}else{
//不覆盖
if(!receivingObject[name]){
receivingObject[name]=givingObject[name];
}
}
}
}else{
throw "Please give 3 arguments at least!";
}
}
3,copyProperties复制属性到对象
/*
* 把属性复制到目标对象中,属性可以有任意多个,并且可以是数组。
*/
function copyProperties(target){
for (var i=1;i<arguments.length;i++){
if(arguments[i].constructor===Array){
copyArrayProperties(target,arguments[i]);
}else{
for(var name in arguments[i]){
target[name] =arguments[i][name];
}
}
}
}
/*
* 把数组格式的属性复制到目标对象中,用户不应该直接调用这个方法
*/
function copyArrayProperties(target,arrayArg){
for(var i=0;i<arrayArg.length;i++){
if(arrayArg[i].constructor===Array){
copyArrayProperties(target,arrayArg[i]);
}else{
for(var name in arrayArg[i]){
target[name] =arrayArg[i][name];
}
}
}
} PS:目前我正打算写一个JavaScript库,用简洁的代码提供很多功能,包括UI组件。