var All = function(s){
this._t = s;
this.Part1 = new All.Part1();
this.Part2 = new All.Part2();
this.Part1._parent = this.Part2._parent = this;
}All.Part1 = function(){
this._part = "part1";
}
All.Part1.prototype.Run = function(){ alert(this._parent._t + ":" + this._part); }All.Part2 = function(){
this._part = "part2";
}
All.Part2.prototype.Run = function(){ alert(this._parent._t + ":" + this._part); }
var o = new All("all1");
o.Part1.Run();
o.Part2.Run();var o2 = new All("all2");
o2.Part1.Run();
o2.Part2.Run();改了一下
this._t = s;
this.Part1 = new All.Part1();
this.Part2 = new All.Part2();
this.Part1._parent = this.Part2._parent = this;
}All.Part1 = function(){
this._part = "part1";
}
All.Part1.prototype.Run = function(){ alert(this._parent._t + ":" + this._part); }All.Part2 = function(){
this._part = "part2";
}
All.Part2.prototype.Run = function(){ alert(this._parent._t + ":" + this._part); }
var o = new All("all1");
o.Part1.Run();
o.Part2.Run();var o2 = new All("all2");
o2.Part1.Run();
o2.Part2.Run();改了一下
但我觉得这样写比较直观
<script type="text/javascript">
var All = function(s){
this._t = s;
this.Part1 = new All.Part1(this);
this.Part2 = new All.Part2(this);
}
All.Part1 = function(e){
this.parent = e;
this._part = "part1";
this.Run = function(){
alert(this.parent._t + ":" + this._part);
}
}
All.Part2 = function(e){
this.parent = e;
this._part = "part2";
this.Run = function(){
alert(this.parent._t + ":" + this._part);
}
}
var o = new All("all1");
o.Part1.Run();
o.Part2.Run();
var o2 = new All("all2");
o2.Part1.Run();
o2.Part2.Run();
</script>
这里是把run写到里面了吧
但Run用原型才对的
如果对象的使用不是很频繁,就是页面只加载几次的那种,就没有必要使用原型.还是以代码的可读性为主楼主的这段代码是用来做动态的模块加载么?类似blog的方式,底层的js架构我也不是很熟悉.
有些大的blog脚本好象就是直接借用window对象做全局管理的对象
我建议使用prototype, 构造函数不要直接使用。除非存放共享数据。