码迷,mamicode.com
首页 > Web开发 > 详细

JS基于原型的继承

时间:2015-05-10 23:50:57      阅读:204      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!