zinherit源码地址:
http://www.nczonline.net/downloads/zInherit1.0.zip
<HTML>
<HEAD>
<TITLE>test </TITLE>
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
Object.prototype.inheritFrom = function (fnClass)
{
/**
* Inherits all classes going up the inheritance chain recursively.
* @param fnClass The class to inherit from.
* @param arrClasses The array of classes to build up.
* @scope private
*/
function inheritClasses(fnClass,arrClasses)
{
arrClasses.push(fnClass);
//alert("type:" + typeof fnClass.__superclasses__ );
if (typeof fnClass.__superclasses__ == "object")
{
for (var i=0; i < fnClass.__superclasses__.length; i++)
{
inheritClasses(fnClass.__superclasses__[i], arrClasses);
}
}
}
if (typeof this.constructor.__superclasses__ == "undefined")
{
this.constructor.__superclasses__ = new Array();
}
inheritClasses(fnClass, this.constructor.__superclasses__);
//所有用prototype方式声明的函数将赋给新实例
for (prop in fnClass.prototype)
{
if (typeof fnClass.prototype[prop] == "function")
{
this[prop] = fnClass.prototype[prop];
}
}
};
/**
* Determines if the given object is an instance of a given class.
* This method is necessary because using {@link #inheritFrom} renders
* the JavaScript <code>instanceof</code> operator useless.
* @param fnClass The constructor function to test.
* @return True if the object is an instance of the class, false if not.
* @scope public
*/
Object.prototype.instanceOf = function (fnClass /*:Function*/) /*: boolean */
{
if (this.constructor == fnClass)
{
return true;
}
else if (typeof this.constructor.__superclasses__ == "object")
{
for (var i=0; i < this.constructor.__superclasses__.length; i++)
{
if (this.constructor.__superclasses__[i] == fnClass)
{
return true;
}
}
return false;
}
else
{
return false;
}
};
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Polygon(iSides){
this.sides = iSides;
}
Polygon.prototype.getArea = function(){
return 0;
};
function Triangle(iBase, iHeight){
Polygon.call(this, 3);
this.base = iBase;
this.height = iHeight;
}
Triangle.prototype.inheritFrom(Polygon);
var t = new Triangle(2, 8);
alert(t.instanceOf(Triangle));
alert(t.instanceOf(Polygon));
//-->
</SCRIPT>
<BODY>
</BODY>
</HTML>对其中的inheritClasses不是理解,其中 inheritClasses(fnClass.__superclasses__[i], arrClasses);
其中fnClass.__superclasses__属性并不存在的,而只有 this.constructor.__superclasses__ = new Array();
难道fnClass.__superclasses__ == this.constructor.__superclasses__ ?
http://www.nczonline.net/downloads/zInherit1.0.zip
<HTML>
<HEAD>
<TITLE>test </TITLE>
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
Object.prototype.inheritFrom = function (fnClass)
{
/**
* Inherits all classes going up the inheritance chain recursively.
* @param fnClass The class to inherit from.
* @param arrClasses The array of classes to build up.
* @scope private
*/
function inheritClasses(fnClass,arrClasses)
{
arrClasses.push(fnClass);
//alert("type:" + typeof fnClass.__superclasses__ );
if (typeof fnClass.__superclasses__ == "object")
{
for (var i=0; i < fnClass.__superclasses__.length; i++)
{
inheritClasses(fnClass.__superclasses__[i], arrClasses);
}
}
}
if (typeof this.constructor.__superclasses__ == "undefined")
{
this.constructor.__superclasses__ = new Array();
}
inheritClasses(fnClass, this.constructor.__superclasses__);
//所有用prototype方式声明的函数将赋给新实例
for (prop in fnClass.prototype)
{
if (typeof fnClass.prototype[prop] == "function")
{
this[prop] = fnClass.prototype[prop];
}
}
};
/**
* Determines if the given object is an instance of a given class.
* This method is necessary because using {@link #inheritFrom} renders
* the JavaScript <code>instanceof</code> operator useless.
* @param fnClass The constructor function to test.
* @return True if the object is an instance of the class, false if not.
* @scope public
*/
Object.prototype.instanceOf = function (fnClass /*:Function*/) /*: boolean */
{
if (this.constructor == fnClass)
{
return true;
}
else if (typeof this.constructor.__superclasses__ == "object")
{
for (var i=0; i < this.constructor.__superclasses__.length; i++)
{
if (this.constructor.__superclasses__[i] == fnClass)
{
return true;
}
}
return false;
}
else
{
return false;
}
};
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Polygon(iSides){
this.sides = iSides;
}
Polygon.prototype.getArea = function(){
return 0;
};
function Triangle(iBase, iHeight){
Polygon.call(this, 3);
this.base = iBase;
this.height = iHeight;
}
Triangle.prototype.inheritFrom(Polygon);
var t = new Triangle(2, 8);
alert(t.instanceOf(Triangle));
alert(t.instanceOf(Polygon));
//-->
</SCRIPT>
<BODY>
</BODY>
</HTML>对其中的inheritClasses不是理解,其中 inheritClasses(fnClass.__superclasses__[i], arrClasses);
其中fnClass.__superclasses__属性并不存在的,而只有 this.constructor.__superclasses__ = new Array();
难道fnClass.__superclasses__ == this.constructor.__superclasses__ ?
解决方案 »
- 关于javascript的一个问题。源代码如下:这段js代码实在看不懂,求高人解释
- 怎么获取这个节点
- 如何在网页中制作一个飘动的图片
- 弹出子窗口需要等待问题
- fck里面的一段代码!始终不清楚是什么意思啊!好象就是因为这个造成js跨域问题啊
- 实时判断表单输入是否合法的问题
- jquery 点击表格中带单选框的一行 单选框选中
- 请问下面的语句中的style="behavior:url(YMcalendar.htc)" 这个风格调用的文件必须是.htc吗?还有怎么来理解这个文件?
- 一个下拉列表框,我希望在他获得焦点的时候自动展开要怎么做?就是document.all.ddlb_1.focus() 的时候
- H5,键盘落下,页面不下来。
- 搜索引擎爬取由javascript加载的页面问题?
- 初学jquery,有一个小问题。向大家请教了。
if (typeof fnClass.__superclasses__ == "object")
{
alert('in:' + fnClass.__superclasses__);
for (var i=0; i < fnClass.__superclasses__.length; i++)
{
inheritClasses(fnClass.__superclasses__[i], arrClasses);
}
}
fnClass.__superclasses__ 的类型总是"undefined",到底这段代码起什么作用,什么时候起作用?
Object.prototype.inheritFrom = function (fnClass)
{
/**
* Inherits all classes going up the inheritance chain recursively.
* @param fnClass The class to inherit from.
* @param arrClasses The array of classes to build up.
* @scope private
**/
function inheritClasses(fnClass,arrClasses)
{
arrClasses.push(fnClass);
//alert("fnClass:" + fnClass + ",type:" + (typeof fnClass.__superclasses__ ));
/** if (typeof fnClass.__superclasses__ == "object")
{
alert('in:' + fnClass.__superclasses__);
for (var i=0; i < fnClass.__superclasses__.length; i++)
{
inheritClasses(fnClass.__superclasses__[i], arrClasses);
}
}**/
}
alert("constructor:" + this.constructor + "superclasses__:" + this.constructor.__superclasses__);
//把所有的父类都放进一个constructor.__superclasses__的一个属性(数组)中,便于instanceOf使用
if (typeof this.constructor.__superclasses__ == "undefined")
{
this.constructor.__superclasses__ = new Array();
}
inheritClasses(fnClass, this.constructor.__superclasses__);
//所有用prototype方式声明的函数(方法)将赋给新实例
for (prop in fnClass.prototype)
{
if (typeof fnClass.prototype[prop] == "function")
{
this[prop] = fnClass.prototype[prop];
}
}
};
/**
* Determines if the given object is an instance of a given class.
* This method is necessary because using {@link #inheritFrom} renders
* the JavaScript <code>instanceof</code> operator useless.
* @param fnClass The constructor function to test.
* @return True if the object is an instance of the class, false if not.
* @scope public
*/
Object.prototype.instanceOf = function (fnClass /*:Function*/) /*: boolean */
{
alert("constructor:" + this.constructor + ",fnClass:" + fnClass);
if (this.constructor == fnClass)
{
return true;
}
else if (typeof this.constructor.__superclasses__ == "object")
{
alert("typeof:" + typeof this.constructor.__superclasses__);
for (var i=0; i < this.constructor.__superclasses__.length; i++)
{
alert("this.constructor.__superclasses__[i] :" + this.constructor.__superclasses__[i] );
if (this.constructor.__superclasses__[i] == fnClass)
{
return true;
}
}
return false;
}
else
{
return false;
}
};
</SCRIPT>被修改后的zInherit源码,没发现任何问题.
请注释中的部分起什么作用?