一句话理解javascript prototype属性
这句话是:prototype中定义的是对象实例要访问的属性或方法的一个替补。
举例说明一下:
//1)定义了一个对象:
function A()
{
//给对象定义一个属性
this.f1="this is f1";
}
//2)我们可以这样使用对象:
var a = new A();
alert(a.f1)//弹出消息:this is f1
//3)我们可以扩展对象:
A.prototype.f1 = "this is new f1";
A.prototype.f2 = "this is f2";
//4)继续使用对象:
alert(a.f1)//弹出消息:this is f1【不是this is new f1】
alert(a.f2)//弹出消息:this is f2
说明:当我们使用对象时,首先从对象的定义中去找对应的属性,找不到再从prototype中去找。
如4)中调用a.f1,就能从对象本身定义中找到f1属性,不会再去prototype中去找f1属性,所以扩展的f1属性就用不上了
而a.f2,则因为在对象本身定义中没有找到f2属性,那么要继续从prototype中寻找有没有f2的定义,找到就返回它
相关文档:
//校验是否全由数字组成
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
function isRegisterUserName(s)
{
v ......
to make a note that I love JavaScript. This article is only meant for some fun, and for us to be aware of some its short-comings.
1. The Name. JavaScript is NOT Java
We'll start with a fun jab at the name choice. While it was originally called Mocha, and then LiveScript, it was later changed to J ......
//创建一个新的元素节点,元素名使用sTagName定义
oElementNode = document.createElementNode(sTagName);
//创建一个新的节点,节点名使用sTextValue定义
oTextNode = document.createTextNode(sTextValue);
//为元素赋一个新的属性,属性名使用sName
oAttribute = document.createAttribute(sName);
//创建一个新的 ......
//复制所选表格的数据到剪贴板 作者:xx
function copyData(){
var content = "";
var tbl = getTbl();
//获取所选的所有数据
for(j = start_Row; j <= end_Row; j++)
{
for(i = start_Col; i <= end_Col; i++)
{
content += (tbl.rows[j].cells[i].childNodes[0].value);
if(i != end_ ......
javascript捕获窗口关闭事件有两种方法
1.用javascript重新定义 window.onbeforeunload() 事件
在javascript里定义一个函数即可
function window.onbeforeunload() { alert("关闭窗口")}
alert()事件将会在关闭窗口前执行,你也可以用户决定是否关闭窗口
function window.o ......