我是靠谱客的博主 迅速灰狼,这篇文章主要介绍javascript 面向对象思想 附源码,现在分享给大家,希望可以做个参考。

复制代码 代码如下:

<html>
<head>
<script type="text/javascript"><!--
ClassModel = //类模型,用于创建类
{
create: function()
{
return function(){this.construct.apply(this,arguments);}
}
}
Extend = function(desc, src) //模拟类继承, 将一个对象的所有成员 复制到 另一个对象中
{
for(var c in src)
{
desc[c] = src[c];
}
return desc;
}
Object.prototype.extend = function(src)
{
return Extend.apply(this, [this, src]);
}
var human = ClassModel.create();
human.prototype =
{
construct : function() //构造函数
{
//alert("construct method");
//alert(this.speak() + "," + this.sleep());
},
speak : function()
{
alert("speak");
},
sleep : function()
{
alert("sleep");
},
sex : function()
{
alert("女");
}
}
var h = new human();
h.speak(); //调用human类的speak方法
var student = ClassModel.create();
student.prototype = (new human()).extend({ //student类继承类human类
sex : function() //方法重载 (多态)
{
alert("男");
},
study : function()
{
alert("studying");
},
thinking : function()
{
alert("thinking");
}


});
var student = new student();
student.sleep(); //调用 父类(human) 的sleep方法
student.study(); //调用 student的study方法
student.thinking(); //调用 student的thinking方法
student.sex(); //结果为 男 不再是父类的 女

// --></script>
</head>
</html>

最后

以上就是迅速灰狼最近收集整理的关于javascript 面向对象思想 附源码的全部内容,更多相关javascript内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部