关于prototype Chain创建对象和实现继承
来源:互联网 发布:淘宝手机壳好评语30字 编辑:程序博客网 时间:2024/05/23 01:58
今天查阅资料发现有资料说我们在使用原型链(prototype chain)的方式实现继承的时候,是不能实现传递参数的构造方法的,觉得这块很乱就做了几个实验!并发表下实验结果和大家一起讨论:
1.首先从我们使用prototype来创建对象说起
function Parent()
{
Parent.prototype.name="zhaogao";
}
var p=new Parent();
alert(p.name);
和
function Parent()
{
}
Parent.prototype.name="zhaogao";
var p=new Parent();
alert(p.name);
效果是一样的,也就是说Parent.prototype.name="zhaogao";代码的位置对我们创建这个属性是没有影响的。
带参数的情况:
function Parent(name)
{
Parent.prototype.name=name;
}
var p=new Parent("zhaogao");
alert(p.name);
也是可以实现我们的目的2.prototype用于继承
对于Parent不带参数的情况
function Parent()
{
Parent.prototype.name="zhaogao";
}
//Parent.prototype.name="zhaogao";
function Child(){ }
Child.prototype=new Parent();var c=new Child();
alert(c.name);
无论Parent.prototype.name="zhaogao";在Parent构造方法的哪个部分都可以实现继承的功能
对于Parent带参数的情况:
function Parent(name)
{
Parent.prototype.name=name;
}
function Child(){ }
Child.prototype=new Parent("zhaogao");var c=new Child();
alert(c.name);
也是可以实现继承的上面的这些都可以正常工作
但是一旦我将Child.prototype=new Parent("zhaogao");或者Child.prototype=new Parent();放到Child的构造函数中,就不能正常工作了
即一旦function Child()
{
Child.prototype=new Parent();
//Child.prototype=new Parent("zhaogao");
}
所以得到结论:Child.prototype=new Parent()不能在function Child(){ }的代码块中,所以这种继承方式也就不能传递参数给构造方法!
- 关于prototype Chain创建对象和实现继承
- JavaScript对象的创建和继承实现
- javascript对象的创建和继承实现
- 创建对象和继承
- 用反射机制和prototype实现继承
- 借用实例化和prototype实现继承
- javascript创建自定义对象和prototype
- 关于javascript-prototype继承
- js创建对象和继承
- javaScript 和 prototype类的创建与继承
- javascript prototype继承对象的属性和方法
- 继承和Prototype
- 作用域链(scope chain)和原型链(prototype chain)
- JS如何创建对象和继承对象
- 用prototype继承整个对象
- Javascript利用反射机制和prototype实现类的继承
- Javascript构造函数和prototype实现封装继承
- 对利用反射和prototype实现继承的简单封装
- 大型网站架构不得不考虑的10个问题
- windows 文件保护机制
- 对于java面向对象的一点浅薄认识
- [WebKit]为JavaScript Binding添加新DOM对象的三种方式及实作
- Sturts2的国际化处理
- 关于prototype Chain创建对象和实现继承
- Quartus9.0中仿真时出现no simulation input file assignment specify 解决方法 (转载)
- 题目链接
- 根据status信息对MySQL服务器进行优化
- 设计模式DP(3)抽象工厂
- 【小蒙淘金】3.5午夜金评-晚间快速波动零点行情如何把握?
- LIB和DLL的区别与使用
- Rss标准格式
- RSS文章订阅及生成RSS格式的xml