javascript模拟C#继承练习
2016-05-29 16:05
417 查看
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <script type="text/javascript"> var o={ say:function() { if (this.name && this.age && this.sex) { alert('名字叫:'+this.name+',今年:'+this.age+'岁了,是个'+this.sex+'的'); } else { alert('参数不完整'); } }, getName:function() //仿C#属性 { if (this.name) return this.name; }, setName:function(name) { if (name){ this.name=name; } }, getAge:function() { if (this.age) return this.age; }, setAge:function(age) { if (age) { this.age=age; } }, setSex:function(sex) { if (sex){ this.sex=sex; } }, getSex:function() { if (this.sex) return sex; } }; var Person=function(name,age,sex) //构造函数 { this.name=name; this.age=age; this.sex=sex; }; Person.prototype=o; //原型链接 var p1 = new Person("john",18,'男'); var inherited=function(add) { if (add) { this.add=add; } var old=this.say; this.say=function() //模拟C#重写函数 { old.apply(this,[]); if (this.add) { alert('地址在'+this.add); } } } p1.say(); //继承前 inherited.call(p1,"XX中街XX号"); //继承p1,增加一个地址 p1.say(); </script> </head> <body> </body> </html>
相关文章推荐
- js学习(内置对象)
- JS window.open()属性
- PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用
- 给 JavaScript 开发者讲讲函数式编程
- JavaScript 跨域的实现方法
- JS中showModalDialog 详细使用
- 用JS获取地址栏参数
- 防止表单重复提交
- JavaScript遍历多维数组
- 封装为一个元素绑定事件
- JavaScript的eval()方法的使用
- js dom元素加载完成执行
- js一篇汇总
- js中random的应用
- 18位身份证号码规则及js验证
- JavaScript中的Function类型总结
- Eclipse如果关闭了JavaScript、xml等文件的校验,我怎么人工去验证某个诸如xml文件格式是否有错误??
- JavaScript的常用内置类
- JS报错:Uncaught SyntaxError: Unexpected identifier
- javascript实现粘贴qq截图功能(clipboardData)