js继承
Person对象类的导出类Person.js代码如下
//创建对象
function Person(name,age){
//给属性表赋值
this.name = name;
this.age = age;
}
//获得对象的名字
Person.prototype.getName = function(){
return this.name;
}
//获得对象的年纪
Person.prototype.getAge = function(){
return this.age;
}
//到对对象
module.exports = Person;
//继承部分
//第一个导出一个类
//导出Person类
let personObj = require("./Person");
//创建普通的对象
let per = new personObj("小熊",22);
console.log(per.getName());
console.log(per.getAge());
//end
//继承
function woman(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
//直接赋值赋值看一下
// woman.prototype = personObj.prototype; 这种方式是错误的
for(var fuc in personObj.prototype){
woman.prototype[fuc] = personObj.prototype[fuc];
}
console.log(woman.prototype)
console.log("woman增加方法")
woman.prototype.getSex = function(){
return this.sex;
}
console.log("woman增加方法后")
console.log(woman.prototype)
//创建对象
let m = new woman("小熊",34,"男");
console.log(m.getName());
console.log(m.getAge());
console.log(m.getSex());
javascript基础教程