Prototype框架的这句该如何理解:subclass.prototype = parent.prototype
代码开始部分:
/* Based on Alex Arnell's inheritance implementation. */
var Class = {
create: function() {
var parent = null, properties = $A(arguments);
if (Object.isFunction(properties[0]))
parent = properties.shift(); function klass() {
this.initialize.apply(this, arguments);
} Object.extend(klass, Class.Methods);
klass.superclass = parent;
klass.subclasses = []; if (parent) {
var subclass = function() { };// parent.prototype并没有定义,如何理解?
subclass.prototype = parent.prototype;
// 我在这里想看看prototype到底是什么,结果只是输出[object Object]。
document.writeln('parent.prototype = ' + parent.prototype + '</br>'); klass.prototype = new subclass;
parent.subclasses.push(klass);
} for (var i = 0; i < properties.length; i++)
klass.addMethods(properties[i]); if (!klass.prototype.initialize)
klass.prototype.initialize = Prototype.emptyFunction; klass.prototype.constructor = klass; return klass;
}
};
代码开始部分:
/* Based on Alex Arnell's inheritance implementation. */
var Class = {
create: function() {
var parent = null, properties = $A(arguments);
if (Object.isFunction(properties[0]))
parent = properties.shift(); function klass() {
this.initialize.apply(this, arguments);
} Object.extend(klass, Class.Methods);
klass.superclass = parent;
klass.subclasses = []; if (parent) {
var subclass = function() { };// parent.prototype并没有定义,如何理解?
subclass.prototype = parent.prototype;
// 我在这里想看看prototype到底是什么,结果只是输出[object Object]。
document.writeln('parent.prototype = ' + parent.prototype + '</br>'); klass.prototype = new subclass;
parent.subclasses.push(klass);
} for (var i = 0; i < properties.length; i++)
klass.addMethods(properties[i]); if (!klass.prototype.initialize)
klass.prototype.initialize = Prototype.emptyFunction; klass.prototype.constructor = klass; return klass;
}
};
解决方案 »
- 学习ajax产生的疑问,关于request.onreadystatechange中右端事件处理器的问题
- 关于将javascript自定义函数封装为类的写法,求救
- 学习.net一段时间,写了一个聊天室,注册用户可以管理好友!欢迎大家测试。
- 用脚本动态设定框架页滚动条:window.mainFrame.scrolling='yes', 为什么不起作用?应该怎么写才可以?
- 有没有办法将<div></div>里面的所有checkbox 都打上钩??
- 谁帮我看看,谢谢。
- 问一个简单的正则表达式
- 关于图片显示
- 自认为有点难的问题,求解!!
- nuxt项目如何调试
- js汉字转换为拼音/简码
- JS正则表达式请教
这是网上一个人的阅读笔记
var Class = {
//定义一个类
create: function () {
//parent是父类的引用,properties是实参数组
var parent = null , properties = $A(arguments);
//如果实参数组的第一个参数是Function类型的,那么把弹出来给parent
if (Object.isFunction(properties[0]))
parent = properties.shift();
//klass是class的音译吧,用来做create返回值得类
function klass() {
this .initialize.apply(this , arguments);
}
//给klass添加addMethods方法
Object.extend(klass, Class.Methods);
//给klass添加了两个属性superclass和subclasses,如果没有父类superclass为null,否则为父类的引用
//subclasses为子类引用的数组
klass.superclass = parent;
klass.subclasses = [];
//原型继承parent,parent的subclasses数组中添加子类的引用
if (parent) {
var subclass = function () { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
parent.subclasses.push(klass);
}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/baozhifei/archive/2009/06/09/4255996.aspx
就是子类继承父类的原型klass.prototype = new subclass
就是把subclass从parent得到的所有原型实例化(开辟新的内存,断开与parent.prototype的引用)
然后赋给klass.prototype(klass的原型)
解释得不清楚,我想看实例化后的subclass.prototype和klass.prototype,怎么看?
alert(Object.keys(object));
function gorush1(){
this.a1=1;
this.a2=2;
a11: 11;
a22: 22;
}
gorush1.prototype.a3=3;
gorush1.prototype.a4=function(x){return x}; gorush1.a5=5;
gorush1.a6=function(y){return y+1}; // 奇怪的是下面两句中keys不包括a1/a2/a11/a22,只有a5/a6
document.writeln('Object.keys(gorush1) in 1.6-test = ' + Object.keys(gorush1) + '</br>' );
document.writeln('Object.values(gorush1) in 1.6-test = ' + Object.values(gorush1) + '</br>' );
// 下面两句中keys包括a3/a4,倒是符合预期,可见直接 函数名.属性名=... 和 函数名.prototype.属性名=... 添加的内容貌似不是一个层次的属性。
document.writeln('Object.keys(gorush1.prototype) in 1.6-test = ' + Object.keys(gorush1.prototype) + '</br>' );
document.writeln('Object.values(gorush1.prototype) in 1.6-test = ' + Object.values(gorush1.prototype) + '</br>' );