View Code
1 /*###########################################* 2 * 1.通过prototype建立面向对象的JavaScript 3 * 2.基于类的继承 4 ###########################################*/ 5 6 /*##################1#################*/ 7 /*创建交通工具的构造方法*/ 8 function Vehicle() { 9 /*交通工具属性*/ 10 var wheelCounts = 4;//车轮数 11 var curbWeightInPounds = 3000;//车子重量 12 13 this.getWheelCounts = function() { 14 return wheelCounts; 15 } ; 16 this.getCurbWeightInPounds = function() { 17 return curbWeightInPounds; 18 } ; 19 20 this.setWheelCounts = function(wc) { 21 wheelCounts = wc; 22 } ; 23 this.setCurbWeightInPounds = function(cwp) { 24 curbWeightInPounds = cwp; 25 } ; 26 //燃料 27 this.refueling = function() { 28 return "refueiing..."; 29 }; 30 31 //主要任务 32 this.mainTasks = function() { 33 return "Go to school, shop and so on."; 34 }; 35 36 }; 37 38 39 40 /*赛车*/ 41 function SPortsCar() { 42 43 //燃料 44 SPortsCar.prototype.refueling = function() { 45 return "SPortsCar refueiing 3000"; 46 }; 47 48 //主要任务 49 SPortsCar.prototype.mainTasks = function() { 50 return "SPortsCar driving, loking good, driving beach. "; 51 }; 52 53 }; 54 55 /*对象之间的复制**/ 56 function copyObject() { 57 58 this.createCopyObject = function (parent, child) { 59 for(var prototype in parent) 60 { 61 if(!child[prototype]){ 62 child[prototype] = parent[prototype]; 63 } 64 }; 65 }; 66 67 }