使用JS编写一段面向对象的代码

    大概是在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>


原文地址:https://www.cnblogs.com/javawebsoa/p/3052919.html