1、类的创建模型:
var Class=
{
create:function()
{
return function()
{
this.initialize.apply(this,arguments);
}
}
}
可以利用Class.create()创建任意对象,但是该对象必须实现initalize()方法。
2、利用$(id)获取指定id的元素
function $(id)
{
if( arguments.length==1 && "string" == typeof id)
{
return document.getElementById(arguments[0]);
}
}
3、继承
var Extend=function(desc,src)
{
for(var property in src)
{
desc[property]=src[property];
}
return desc;
}
Object.prototype.extend=function(obj)
{
return Extend.apply(this,[this,obj]);
}
动物类和鱼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>广告的无缝滚动</title>
</head>
<script type="text/javascript">
function $(id)
{
if( arguments.length==1 && "string" == typeof id)
{
return document.getElementById(arguments[0]);
}
}
var Class =
{
create: function()
{
return function()
{
this.initialize.apply(this, arguments);
}
}
}
var Extend=function(desc,src)
{
for(var property in src)
{
desc[property]=src[property];
}
return desc;
}
Object.prototype.extend=function(obj)
{
return Extend.apply(this,[this,obj]);
}
var Animal=Class.create();
Animal.prototype={
initialize:function(name) //相当于构造方法
{
this.name=name;
},
walking:function()
{
this.walk;
}
}
var Fish=Class.create();
Fish.prototype=(new Animal()).extend(
{
walk:function()
{
alert(this.name+"正在游");
}
});
window.οnlοad=function()
{
var fish=new Fish("鲤鱼");
fish.walk();
}
</script>
<body>
</body>
</html>