首页 > Web开发 > 详细

JS基于原型的继承

时间:2015-05-10 23:48:19      阅读:399      评论:0      收藏:0      [点我收藏+]

function Person(name , age){
this.name = name;
this.age = age;
}
Person.prototype.hi = function(){
console.log("hi my neame is "+this.name+" , I‘m "+ this.age+" years old!");
}
Person.prototype.LEG_NUM = 2;
Person.prototype.ARAMS_NUM = 2;
Person.prototype.walk = function(){
console.log(this.name + "is working!");
}
function Student(name , age , className){
Person.call(this , name , age);
this.className = className;
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
Student.prototype.hi = function(){
console.log("hi my name is "+ this.name +" , I‘m "+ this.age+" years old and from "+ this.className);
}
Student.prototype.learn = function(subject){
console.log(this.name + " is learning " + subject +" at " + this.className);
}
//test
var bbedword = new Student("bbedword" , 25 , "××大学,计算机科学与技术(4)班");
bbedword.hi();
bbedword.LEG_NUM;
bbedword.ARAMS_NUM;
bbedword.walk();
bbedword.learn("前端开发JS");

运行结果如下:

技术分享

 

 

来源于慕课网学习!!!

JS基于原型的继承

原文:http://www.cnblogs.com/bbedword/p/4493389.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!