16. 承继
var Student = Object.create(Person);
Student.getName = function(){
return "Stu Name: " + this.firstname + this.lastname;
}
Student.doHomework = function(){
console.log("I am doing my homework");
}
var fullname = Student.getName();
console.log(fullname);
Student.walking();
Student.doHomework();
17. 造函数法构
var Person = function(fname,lname,sex){
var firstname = fname;
var lastname = lname;
this.getName = function(){
return firstname + lastname;
}
this.sexual = sex;
Person.prototype.sayHello = function(){
if(this.sexual == "male"){
console.log("Hey, I am a man!")
}else{
console.log("Hi, I am a woman~")
}
}
}
var p = new Person('Rex','Gao','male');
console.log(p.getName());
p.sayHello();
19. 再 承谈继
var Student= function(fname,lname,sex){
Person.apply(this, arguments);
};
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
Student.prototype.sayHello = function(){
if(this.sexual == "male"){
console.log("Momoda, I am a boy!")
}else{
console.log("Oba, I am a girl~")
}
}
Student.laugh = function(){
console.log("Hahaha~~");
}
var s = new Student('Rex','Gao','male');
console.log(s.getName());
s.sayHello();
Student.laugh();