Javascript中的利用原形链和对象冒充创建类
看到一个曾经搞过web的人的blog中说到如果学Javascript不懂原形链,就太遗憾了,所以当自己看《javascript高级程序设计》时就留意了一下,说实话,下面的代码很简单,但是不是很懂所谓的原形链和对象冒充之类的东西 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type" />
<title>继承机制的实现--实例</title>
<script type="text/javascript">
function Polygon(iSides){
this.sides=iSides;
}
Polygon.prototype.getArea=function(){
return 0;
};
function Triangle(iBase,iHeight){
Polygon.call(this,3);
this.base=iBase;//设置基本的边数
this.height=iHeight;
}
Triangle.prototype=new Polygon();
Triangle.prototype.getArea=function(){
return 0.5*this.base*this.height;
};
function Rectangle(iWidth,iHeight){
Polygon.call(this,4);
this.width=iWidth;
this.height=iHeight;
}
Rectangle.prototype=new Polygon();
Rectangle.prototype.getArea=function(){
return this.width*this.height;
};
var triangle=new Triangle(12,4);
var rectangle=new Rectangle(22,10);
alert("三角形的边
相关文档:
JavaScript 有六种数据类型。主要的类型有 number、string、object 以及 Boolean 类型,其他两种类型为 null 和 undefined。
String 字符串类型:字符串是用单引号或双引号来说明的。(使用单引号来输入包含引号的字符串。)如:“The cow jumped over the moon.”
数值数据类型:JavaScript 支持整数和浮点 ......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id ......
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table
2. <body onselectstart="return false"> 取消选取、防止复制
3. onpaste="return false" 不准粘贴
4. oncopy=& ......
/* 方法:Array.remove(dx)
* 功能:删除数组元素.
* 参数:dx删除元素的下标.
* 返回:在原数组上修改数组
*/
//经常用的是通过遍历,重构数组.
Array.prototype.remove=function(dx)
{
if(isNaN(dx)||dx>this.length){return false;}
for(var i=0,n=0;i<this.length;i++)
{
......