易截截图软件、单文件、免安装、纯绿色、仅160KB

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类

// 下拉列表类
// PubEdition: Version 1.0.0.0
// ModifyDate: 2009-12-09 9:30:00
// FinishDate: 2009-12-09 9:30:00
// AuthorName: binbin( Mail:pl45@163.com / Tel:13893302154 )
var droplistswap=function(){};
droplistswap.prototype.originalElement=new Array();
// droplistswap.prototype.hidedropl ......

javascript运行、调试工具

      最近在学习javascript,对于如何运行调试却不了解。以为在记事本中编辑好文件后保存为htm文件,然后在浏览器中打开就可以了,但我试了多次都不成功,后来终于发现原来我在记事本中编辑保存的文件的后缀名为“.txt”。
     解决方法:“文件夹选项&rdqu ......

进一步理解javascript对象、数组和哈希表

在javascript中,对象实际上就是一个哈希表,比如下面这个user对象:
function user(n, a)
{
this.name = n;
this.age = a;
this.toString = function() {
return "Name:" + this.name + ", Age:" + this.age;
}
}
var u = new user("tom", 18);
for (var k in u) {
alert('key: ' ......

关于javascript内存泄露

<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函数


http://xiayuanfeng.javaeye.com/blog/(原文)
什么是函数(Function)
function sum(a,b){  
     return a+b;  
}  
其实通俗的说就是一个有名称的代码段,方便重用。
要注意的是:
1.Javascript 的函数语法,因为Javascript本身就是区分大小写的,所以function不能写作Function或 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号