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
相关文档:
1 Add the following code to .aspx
<script>
function fresh() {
{
window.opener.document.getElementById("ControlId").click(); //ControlId -- ......
在Javascript中作用域是由函数划分的。
//设置全局变量foo
var foo = "test";
if(true){
var foo = "new foo";
}
//此时foo为"new test"
alert(foo == "new foo");
function test(){
var foo = "old test";
}
//调用时,foo只在函数作用域内起作用
test();
//foo还是等于"new test"
alert(foo == "new te ......
在类的构造函数中添加
EnableAutomation();
在OnInitDialog中添加
SetExternalDispatch(GetIDispatch(TRUE));
在类的声明中添加宏
DECLARE_DISPATCH_MAP()
在类的实现文件中添加组宏
BEGIN_DISPATCH_MAP(当前类, 基类)
END_DISPATCH_MAP()
然后就可以用 DISP_FUNCTION宏来映射导出函 ......
在 document 对象中有一个 cookie 属性。但是 Cookie 又是什么?“某些 Web 站点在您的硬盘上用很小的文本文件存储了一些信息,这些文件就称为 Cookie。”—— MSIE 帮助。一般来说,Cookies 是 CGI 或类似,比 HTML 高级的文件、程序等创建的,但是 JavaScript 也提供了对 Cookies 的很全面的访问权利 ......