javascript中的prototype使用起来感觉变化莫测,不是很好理解。现总结一下,以便日后查阅。
1.prototype只能通过类名(或者叫function名)来访问
2.父类中的变量只能通过子类名.prototype.变量名
的方式来修改,通过子类的对象只能读父类中的变量,如果修改的话,其实修改的是子类自己的同名变量
3.通过子类的对象读取一个变量时,先在子类中找,如果子类中没有,再到父类中找
4.每个子类对象都持有同一个父类的引用
比如:
function ClassA()
{
this.a='a';
this.f=function(){alert();};
}
function ClassB()
{
this.b='b';
this.sf=function(){alert();};
}
ClassB.prototype=new ClassA();
//ClassB继承自ClassA
var objB1=new ClassB();
var objB2=new ClassB();
alert(objB1.f == objB2.f); //参照同一个父类的方法
alert(objB1.sf == objB2.sf); //参照各自的子类方法
alert(objB1.a); //因为ClassB中没有a,所以访问父类的变量a
objB1.a='x';
//ClassB中增加变量a,值为x,和父类中的变量a没有关系
ClassB.prototype.a='y';
//将ClassA中的a的值修改为y
alert(objB1.a); //因为ClassB中有a,所以访问子类的变量a
输出:
true
false
a
x
5.可以通过子类名.prototype.变量名
的方式来修改父类中的变量,或者向父类中增加变量
也可以通过子类的对象名.变量名
的方式来修改子类中的变量,或者向子类中增加变量。
比如:
function ClassA()
{
this.a='a';
}
function ClassB()
{
this.b='b';
}
ClassB.prototype=new ClassA();
var objB1=new ClassB();
objB1.c='c'; //objB1中增加变量c
ClassB.prototype.d='d'; //父类的对象中增加变量d
alert(objB1.c);
alert(objB1.d);
输出:
c
d
6.给prototype赋值的注意事项
方法1:
function ClassA()
{
this.a='a';
}
function ClassB()
{
this.b='b';
}
//ClassB.prototype相当于ClassA的一个对象
ClassB.prototype=new ClassA();
var objB1=new ClassB();
//修改ClassA中的变量a
ClassB.prototype.a='a1';
//向ClassA中增加变量c
ClassB.prototype.c='c';
alert(objB1.a);
alert(objB1.c);
输出:
a1
c
方法2:
function ClassA()
{
this.a='a';
}
function ClassB()
{
this.b='b';
}
ClassB.prototype.x='x';
ClassB.prototype.y='y';
var objB1=new ClassB();
//创建对象时已经创建了变量x和y
alert(objB1.x);
alert(objB1.y);
//此时objB1已经创建完,所以ClassA不是ClassB的父类
ClassB.prototype=new ClassA();
//追加父类时原来的变量x和y不会丢失
alert(objB1.x);
alert(objB1.y);
//ClassA不是ClassB的父类
alert(objB1.a);
输出:
x
y
x
y
undefined
方法3:
function ClassA()
{
this.a='a';
}
function ClassB()
{
this.b='b';
}
ClassB.prototype.x='x';
ClassB.prototype.y='y';
ClassB.prototype=new ClassA();
var objB1=new ClassB();
//new ClassA()时x和y被覆盖
alert(objB1.x);
alert(objB1.y);
输出:
undefined
undefined
分享到:
相关推荐
prototype1.6.0prototype1.6.0prototype1.6.0prototype1.6.0
prototype的JS文件prototype的JS文件prototype的JS文件prototype的JS文件
Prototype Codes TextBook.rar:Prototype实战教程(爆爽,推荐); 2.Prototype API: Prototype1.5.1_API_CN_A.CHM:Prototype1.5.1中文API,版本一(推荐); prototype1.5.1_API_CN_B.CHM:Prototype1.5.1中文API,版本二...
对prototype的用法说明
prototype js脚本库prototype js脚本库prototype js脚本库prototype js脚本库
1、 Prototype-1.6.0 中文版 2、Prototype-1.6.0 英文版 3、Prototype.js
prototype-1.6.0.3.js prototype1.4中文手册(chm) prototype1.5中文手册(chm) prototype1.6英文手册(chm)
prototype.js 是Sam Stephenson写的一个非常优雅的java script基础类库,对java script做了大量的扩展,而且很好的支持Ajax,兼容标准的类库,能帮助你轻松建立有高度互动的web2.0特性的丰富的客户端页面。...
prototype帮助中文文档,prototype的源代码
prototype prototype 1.6中文手册 prototype 1.6中文手册chm 非常详尽,爱不释手
prototype的Ajax介绍
prototype 1.3 源码解读
Prototype中文帮助文档Prototype中文帮助文档
prototype1.5.1
Prototype中英文手册 Prototype中文手册 Prototype英文手册 Prototype API
找了好久,才找到下载资源 prototype_1.7.3.js 最新版本
prototype是函数对象上面预设的对象属性 1. JS中所有的东西都是对象,每个对象都有prototype这个属性,这个属性是一个对象(object) 2. JS中所有的东西都由Object衍生而来, 即所有东西原型链的终点指向Object....
非常实用的prototype 例子,对于快速掌握prototype有很好的帮助