????????本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小生感激不尽。
????????本篇文章为您分析一下原生JS的对象继承方法
1. 普通用户功能
2. 会员用户功能
3. 会员用户需要拥有普通用户的所有功能,并且自身也要有自己的功能
/**
* 普通用户构造函数
* @param {*} firstName
* @param {*} lastName
* @param {*} age
*/
function User(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.fullName = this.firstName + " " + this.lastName;
}
// 普通用户构造函数的方法
User.prototype.sayHello = function () {
console.log(`大家好,我叫${this.fullName}, 加年${this.age}了`);
}
/**
* 会员用户构造函数
* @param {*} firstName
* @param {*} lastName
* @param {*} age
* @param {*} money
*/
function VIPUser(firstName, lastName, age, money) {
User.call(this, firstName, lastName, age);
this.money = money;
}
VIPUser.prototype.upgrade = function () {
console.log(`使用了${100}元软妹币升级了`);
this.money -= 100;
}
var vUser = new VIPUser("晓", "佰", 10, 100); // 调用构造函数
??
??
/**
* 对象的继承
* @param {Function} son 子类构造函数
* @param {Function} father 父类构造函数
*/
this.myPlugin.inherit = function (son,father) {
son.prototype = Object.create(father.prototype);
son.prototype.constructor = son;
son.prototype.uber = father.prototype;
}
??
??
??
??!
??
??
/**
* 雅虎公司的继承
* @param {Function} son 子类构造函数
* @param {Function} father 父类构造函数
*/
this.myPlugin.inherit = (function () {
var Temp = function () { };
return function (son, father) {
Temp.prototype = father.prototype;
son.prototype = new Temp();
son.prototype.constructor = son;
son.prototype.uber = father;
}
}());
整完!!
原生JavaScript函数对象的继承 inherit 带 插件完整解析版[helpers.js]
原文:https://www.cnblogs.com/tewutuyo/p/12839881.html