使用JS编写一段面向对象的代码
2013-05-01 11:17
309 查看
大概是在2009年吧,去参加了一个公司的笔试题目,题目的内容是“使用JS编写一段面向对象的代码”,因为在这家公司待得时间太长了,很少碰到JS方面的编程需求,所以这个题目居然没有回答上来,最近趁着假期,重温了JavaScript相关的知识,把这个题目的解答写在这里,算是对几年前的这个考试的一个纪念好了。
<script type="text/javascript"> function Animal(aname,age,weight){ this.aname = aname; this.age = age; this.weight = weight; } function Animal.prototype.getAname(){ return this.aname; } function Animal.prototype.setAname(aname){ this.aname = aname; } function Animal.prototype.getAge(){ return this.age; } function Animal.prototype.setAage(age){ this.age = age; } function Animal.prototype.getWeight(){ return this.weight; } function Animal.prototype.setWeight(){ this.weight = weight; } var animal = new Animal('Andy',13,13); document.write(animal.getAge()); animal.setAname('Chenzw'); document.write(animal.getAname()); </script>
相关文章推荐
- 使用JS编写一段面向对象的代码
- 使用JS编写一段面向对象的代码
- 一段简单的使用js在web页面中显示日期时间的代码
- 分享一段JS代码,个人感觉很使用:网页浏览历史记录
- 如何使用 require.js ,实现js文件的异步加载,避免网页失去响应,管理模块之间的依赖性,便于代码的编写和维护。
- 题型:编写一段JS代码提取URL中的各个GET参数,并按Key_Value的形式返回到一个对象中(如:Json结构)
- 通过一段代码简单说js中的this的使用
- 通过一段代码简单说js中的this的使用
- 分享一段JS代码,个人感觉很使用:网页浏览历史记录
- 【asp.net 】使用js结合hidden控件实现在后台运行一段程序后提示确认,确认通过后继续执行后台代码~~~
- 使用flow捕获js错误提高代码质量
- 使用JS判断单选按钮和复选框是否被选中的代码
- java学习-【转】如何用Java编写一段代码引发内存泄露
- 搜集了一段播放用java播放.wav的代码,可以直接使用
- 想用JS写一段鼠标拖拽调整图片大小的代码(未完)
- 编写高质量代码改善C#程序的157个建议——建议89:在并行方法体中谨慎使用锁
- 一段JS格式化JSON的代码
- 一段用于编辑页面表单并使用ajax提交与无刷新获取的js段落