Posted on 2007-04-06 13:32
itspy 阅读(2027)
评论(5) 编辑 收藏
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Prototype </TITLE>
<SCRIPT LANGUAGE="JavaScript">
//此函数来至于prototype,由于此代码只是简单的演示,所以就直接把其提取出来了.
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
function Student(major){
this.major=major;
}
Object.extend(Student.prototype, {
sayHello: function(){
alert("Hello world!");
}
}
)
/**======================================
我比较奇怪的是上面的函数调用下面这段替换怎么就不行
function People(){
sayHello= function(){
alert("Hello world!");
}
}
var people= new People();
var stu=new Student("manage");
Object.extend(stu,people);
=========================================**/
var stu=new Student("manage");
/**=================================
通过这种方式虽然student继承了pepole的属性,但通过instanceof,最后的属性并不是people
==================================**/
stu.sayHello();
for (var property in stu) {
alert(property);
}
</SCRIPT>
</HEAD>
<BODY>
本代码最初发表于 http://blog.openj.cn
</BODY>
</HTML>