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 ......
很多人都向在服务器端调用客户端的函数来操作,也就是在asp中调用javascript脚本中已经定义好的脚本函数。经过研究,发现了一些勉强的方法。
1. 用Response.Write方法写入脚本
比如在你单击按钮后,先操作数据库,完了后显示已经完成,可以在最后想调用的地方写上
Response.Write("<script type='text/javascrip ......
getkey 用于获得按键的相应代码, getev用于获得响应事件
<script>
function getkey(e)
{
var key;
if(document.all)
{
  ......
var o = ['你', 'da', '你', 123, 'fm', 'fm', 'da', 123, 'fm', 123];
var n = [];
var s = -1;
var t = null;
o.sort();
for(var i = 0; i < o.length; i++)
{
if(o[i] != t)
{
s += 1;
n[s] = [];
}
......