搞了半天没清楚 先留下存档
<!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 http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function Student()
{
this.d="d";
this.show2=function(){alert("show2");}
//为什么不报错 也不执行 代码已经解析 但是不执行 查看源文件
//this.prototype.protoPrint(return "protoPrint");
//Student.prototype.protoPrint(return "protoPrint");
}
Student.prototype.show3=function(){alert("show3");}
Student.prototype.s1="11";
Student.prototype.s2="22";
Student.prototype.println1=function(){return "println1";}
Student.prototype.println2=function(){return "println2";}
//静态方法
Student.show=function()
{
alert("this is static!");
}
var stu1=new Student();
var stu2=new Student();
alert(stu1.println1());
alert(stu2.println1());
//改变类的原型方法 所有实例对象(2个)全部改变
//是引用??还是所有实例对象都共享一个原型方法????
//如果后者,那内存中到底有几个对象???2个还是3个(+原型对象)??
Student.prototype.println1=function(){return "1";};
alert(stu1.println1());
alert(stu2.println1());
alert(stu1.protoPrint());
//实例对象不能调用静态属性和方法
//Student.s3="33";
//alert(stu1.s3);
//alert(Student.s3);
</script>
</head>
<body>
</body>
</html>
另外 在51cto找到点资料 好理解了,但是还是不能够完全理解透彻 ,脑子晕
JavaScript的Prototype属性
The JavaScript Prototype Property
JavaScript没有提供传统的类结构,使你可以通过继承它,然后加入新功能来扩展一个类。相反,这门语言使用prototype属性来扩展现有对象,以及它们的示例。
prototype是在运行期定义的一个属性和方法的集合,它对对象的每个示例都是有效的,而且不管这些示例是在prototype修改前,还是修改后建立的。
prototype在JavaScript中的工作原理是:当访问对象的属性时,浏览器的脚本引擎首先会从本地属性(native propertie)中查找这个属性,接着会在prototype属性中进行查找。如果在prototype属性中没有找到,它就会检查示例级的属性。