JavaScript:prototype属性使用方法
一、基本使用方法
prototype属性可算是JavaScript与其他面向对象语言的一大不同之处。
简而言之,prototype就是“一个给类的对象添加方法的方法”,使用prototype属性,可以给类动态地添加方法,以便在JavaScript中实现“继承”的效果。
具体来说,prototype 是在 IE 4 及其以后版本引入的一个针对于某一类的对象的方法,当你用prototype编写一个类后,如果new一个新的对象,浏览器会自动把prototype中的内容替你附加在对象上。这样,通过利用prototype就可以在JavaScript中实现成员函数的定义,甚至是“继承”的效果。
一个简单的示例如下:
view plaincopy to clipboardprint?
Number.prototype.add = function(num){return(this+num);}
Number.prototype.add = function(num){return(this+num);}
这是对已有类添加方法。这样写,可以增强已有类的功能,例如可以给Array类增加push方法如下:
view plaincopy to clipboardprint?
Array.prototype.push = function(new_element){
this[this.length]=new_element;
return this.length;
}
Array.prototype.push = function(new_element){
this[this.length]=new_element;
return this.length;
}
对于自定义的类(或者称函数对象),也可以这样写:
view plaincopy to clipboardprint?
function MyApplication() {
this.counter = 0;
this.map = new GMap2(document.getElementById("map_canvas"));
this.map.setCenter(new GLatLng(39.917,116.397), 14);
GEvent.bind(this.map, "click", this, this.onMapClick);
}
MyApplication.prototype.onMapClick = fu
相关文档:
Javascript函数包含一个伪数组(pseudo-array),该数组包含所有传入参数,我们不能修改它,但可以访问其中的成员,该数组也具有length属性。
函数重载依赖于判断传入参数个数和类型的能力。
函数重载的例子:
<html>
<head>
<mce:script type="text/javascript"><!--
function sendMessage(msg ......
一般事件 事件 浏览器支持 描述
onClick IE3|N2|O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击
onDblClick IE4|N4|O 鼠标双击事件
onMouseDown IE4|N4|O 鼠标上的按钮被按下了
onMouseUp IE4|N4|O 鼠标按下后,松开时激发的 ......
这两天的工作中遇到一个有关js的问题,很郁闷遇到js问题,因为没有报错,你根本就不知道自己错在哪里。
其实就是关于“document.getElementById("ss").innerText”的问题,我上网查关于js浏览器的兼容问题,可是都给出的解释是:
“HTML对象获取问题
FireFox
:document
.getElementById
(& ......
javascript调用父窗口(父页面)的方法
window.parent与window.opener的区别 javascript调用主窗口方法
1: window.parent 是iframe页面调用父页面对象
举例:
a.html
Html代码
<html>
<head><title>父页面</title></head> &nb ......