Javascript继承机制(call、apply、prototype)
Javascript的继承可以通过call、apply、prototype实现。
1、call:在子类中,用父类.call(this,arg0,arg1...)可以继承父类。注意call的位置,尽量在子类的第一行(js按顺序执行,放在后面可能对子类的其他属性、方法有影响。比如子类和父类有相同名字的方法,后面的覆盖前面的)。
<html>
<head>
<title>call\apply\prototype test</title>
</head>
<script type="text/javascript">
//person类
function person(name, age) {
this.name = name;
this.age = age;
this.say = function() {
document.write("I am a person");
}
this.display = function() {
document.write(this.name + "-" + this.age);
}
}
//student类,继承自person
function student(name, age, no) {
person.call(this, name, age);//person中的this等于参数中的this,即student中的name和age
this.no = no;
this.display = function() {//覆盖了父类的方法
document.write(this.name + "-" + this.age + "-" + this.no);
}
}
//创建person类
var p = new person("captain", 21);
p.display();//captain-21
p.say();//I am a person
//创建student类
var s = new student("captain", 21, "06281097")
s.display();//captain-21-06281097
s.say();//I am a person 继承了父类的方法
<script>
<body>
</body>
</html>
2、apply:在子类中,用父类.apply(this,args)可以继承父类(args为参数数组)。
<html>
<head>
<title>call\apply\prototype test</title>
</head>
<script type="text/javascript">
//person类
function person(name, age) {
this.name = name;
this.age = age;
this.say = function() {
document.write("I am a person");
}
this.display = function() {
document.write(this.name + "-" + this.age);
}
}
//student类,继承自person
function student(name, age, no) {
person.apply(this, new Array(name, age));//与call不同的是,apply传递的是数组
this.no = no;
this.display = function() {//覆盖了父类的方法
document.write(this.name + "-" + this.age + "-" + this.no);
}
}
//创建pe
相关文档:
*说明:select元素javascript常用操作
* 1.判断是否存在指定value的Item
* 2.加入一个Item
* 3.删除值为value的所有Item
* 4.删除某一个index的选项
* 5.更新第index项的value和text
* 6.设置select中指定text的第一个Item为选中
* 7.设置select中指定value的第一个Item为选中
* 8.得到当前选中项的v ......
这段时间在用displayTag, 它有很多好用的功能,俺也不就在这赘述,当然也有几个不方便的地方. 比如求和后"%"的显示等与求和相关的缺点, 这里先介绍一个缺点并把我从网上整理出来的解决方案也一并帖出. 问题是这样的: 由于业务要求, 当用户鼠标位于表中某一列的标头时, 在这个表头"动画"地显示这列的简介. 先看下做出来的效果: ......
<html>
<body>
<script type="text/JScript">
for (i=0; i<10000; i++) { // this loop enforces the effect
var model = new Object();
var element = document.createElement("<br>");
model.myElement = ......
用Javascript可以实现对GridView中某一行选择,而无需进行页面的刷新。
首先要在GridView的onrowdatabound的event handler中为GridView中的每一行onclick绑定event handler (Javascript)。假如GridView代码如下:
<asp:GridView runat="server" id="GridViewCategory" Aut ......
Several programming languages implement a sprintf function, to output a formatted string. It originated from the C programming language, printf function. Its a string manipulation function.
This is limited sprintf Javascript implementation. Function returns a string formatted by the usual printf co ......