請使用javascript類比對象,建立Person類,要求有姓名和年齡屬性,然後使用繼承實現Programmer類,要求有姓名、年齡、性別以及掌握的 語言屬性。
以下為實現代碼(此類繼承用的是原型鏈實現):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>New Web Project</title> <style type="text/css"> span{ color: #66A300; font-family: "Arial Black"; } </style> </head> <body> <div id="main"> <span>hello!</span> </div> <script type="text/javascript"> function Person(name,age){ this.name = name; this.age = age; } function Programmer(name,age,sex,language){ Person.call(this,name,age);//繼承類的建構函式 this.sex = sex; this.language = language; } //設定原型鏈 Programmer.prototype = new Person(); Programmer.prototype.constructor = Programmer; Programmer.prototype.getName = function(){ return this.name; } var smirk = new Programmer("zy",21,"f","ENGLISH"); alert(smirk.getName()); </script> </body></html>