Javascript 之prototype属性
在js中,每个对象都有一个prototype属性:返回对象类型原型的引用。很拗口!习语“依葫芦画瓢”,这里的葫芦就是原型,那么“瓢.prototype” 返回的就是葫芦,或者“瓢.prototype= new 葫芦()”。
prototype的用途:
继承
有一个对象--子类:
function 子类() {
this.lastname = "Samuel";
}
有一个对象--父类:
function 父类() {
this.firstname = "Shen";
}
现在子类是有名无姓,父类是有姓无名,如果子类要有名有姓的话,只要说明--子类的原型是父类--就可以了,即子类继承自父类:
子类.prototype = new 父类();
至此,子类就不再是有名无姓了。
alert(子类.firstname + " " + 子类.lastname); //Samuel Shen
牵一发而动全身
既然prototype返回的是原型的引用,那么如果改变原型的话,所有继承自该原型的对象都将受到影响。
function Point(x,y) {
this.x = x;
this.y = y;
}
var p1 = new Point(1,2);
var p2 = new Point(3,4);
Point.prototype.z = 0; //动态为Point的原型添加了属性
alert(p1.z); //0
alert(p2.z); //0
/***************************************************************************/
function Person(name,sex) { //Person类的构造函数
this.name = name;
this.sex = sex;
}
Person.prototype.age = 12; //为Person类的prototype属性对应的prototype对象的属性赋值,
//相当于为Person类的父类添加属性
Person.prototype.print = function() { //为Person类的父类添加方法
alert(this.name+"_"+this.sex+"_"+this.age);
};
var p1 = new Person("name1","male"); //p1的age属性继承子Person类的父类(即prototype对象)
var p2 = new Person("name2","male");
p1.print(); //name1_male_12
p2.print(); //name2_male_12
p1.age = 34; //改变p1实例的age属性 p1属性在不指向Person的prototype属性
p1.print(); //name1_male_34
p2.print(); //name2_male_12
Person.prototype.age = 22; //改变Person类的超类的age属性
p1.print(); //name1_male_34(p1的age属性并
相关文档:
重点在于function scroll(),function clipShow()及以下for循环。
无缝滚动新闻的Javascript源代码,放在这里,有需要的时候可能用得上:
//CSS样式
<style>
.new_newsT{
padding-top: 10px;
padding-bottom: 8px;
}
.new_newsT .list {
CLEAR: both; MARGIN: 0px 6px 0px 10px
}
......
表单的客户端javascript验证有各种各样的写法,登录为form的onsubmit事件或submit按扭写一个函数。对于小表单(只有一两个表单域的表单)就不必再专门再用javascript写一个验证函数了,只需要在form的onsubmit事件里加上:
onsubmit=”return domainname.value==”?(alert(’请输入搜索内容’),false ......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>vForm表单验证程 ......
AA.HTM
-------------------------------------
<!--
showModalDialog函数的使用 (转)
本范例可以实现弹出一个模态窗口,并演示了两种接收和传递参数的方法,同时可以接受模态窗口返回的多个变量
-->
<html> &nbs ......