您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下javascript中实现继承的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
//声明父类
//声明父类
function SuperClass() {
this.superValue = true;
}
//为父类添加共有方法
SuperClass.prototype.getSuperValue = function () {
return this.superValue;
};
//声明子类
function SubClass() {
this.subValue = false;
}
//继承父类
SubClass.prototype = new SuperClass();
//为子类添加共有方法
SubClass.prototype.getSubValue = function () {
return this.subValue;
};
var instance = new SubClass();
console.log(instance.getSuperValue()); //true
console.log(instance.getSubValue()); //false
function SuperClass() {
this.courses = ['语文', '数学', '英语']
}
function SubClass() {}
SubClass.prototype = new SuperClass();
var instance1 = new SubClass()
var instance2 = new SubClass()
console.log(instance2.courses) //['语文', '数学', '英语']
instance1.courses.push('化学')
console.log(instance2.courses) //['语文', '数学', '英语', '化学']
function SuperClass(current) {
this.courses = ["语文", "数学", "英语"];
this.current = current;
}
//父类声明原型方法
SuperClass.prototype.getCourses= function () {
console.log(this.courses);
};
//声明子类
function SubClass(current) {
SuperClass.call(this, current);
}
var instance1 = new SubClass("语文");
var instance2 = new SubClass("数学");
instance1.courses.push('化学')
console.log(instance1.courses); //["语文", "数学", "英语", "化学"]
console.log(instance1.current); //语文
console.log(instance2.courses); //["语文", "数学", "英语"]
console.log(instance2.current); //数学
instance1.getCourses() //TypeError: instance1.getCourses is not a function
//组合继承
function SuperClass(current) {
//引用类型共有属性
this.courses = ["语文", "数学", "英语"];
// 值类型共有属性
this.current = current;
}
SuperClass.prototype.getCourses = function () {
console.log(this.courses);
};
SuperClass.prototype.getCurrent = function () {
console.log(this.current);
};
// 声明子类
function SubClass(current, time) {
//构造函数继承父类属性
SuperClass.call(this, current);
this.time = time;
}
//类式继承 子类原型继承父类
SubClass.prototype = new SuperClass();
//子类原型方法
SubClass.prototype.getTime = function () {
console.log(this.time);
};
var instance1 = new SubClass("语文", "9:00");
instance1.getTime(); //9:00
instance1.courses.push('化学')
instance1.getCourses(); //["语文", "数学", "英语", "化学"]
instance1.getCurrent(); //语文
console.log(instance1.current)//语文
var instance2 = new SubClass("数学", "10:00");
instance2.getTime(); //10:00
instance2.getCourses(); //["语文", "数学", "英语"]
instance2.getCurrent(); //数学
console.log(instance2.current)//数学
function inheritObject(o) {
function F() {}
F.prototype = o;
return new F();
}
var course = {
name: "语文",
alikeCourse: ["数学", "英语"],
};
var newCourse = inheritObject(course);
newCourse.name = "化学";
newCourse.alikeCourse.push("物理");
var otherCourse = inheritObject(course);
otherCourse.name = "政治";
otherCourse.alikeCourse.push("历史");
console.log(newCourse.name); //化学
console.log(newCourse.alikeCourse); //["数学", "英语", "物理", "历史"]
console.log(otherCourse.name); //政治
console.log(otherCourse.alikeCourse); //["数学", "英语", "物理", "历史"]
console.log(course.name); //语文
console.log(course.alikeCourse); //["数学", "英语", "物理", "历史"]
function inheritObject(o) {
function F() {}
F.prototype = o;
return new F();
}
var course = {
name: "语文",
alikeCourse: ["数学", "英语"],
};
function createCourse(obj) {
//通过原型继承方式创建新对象
var o = new inheritObject(obj);
// 拓展新对象
o.getName = function () {
console.log(this.name);
};
return o;
}
const newCourse = createCourse(course)
function inheritObject(o) {
function F() {}
F.prototype = o;
return new F();
}
function inheritPrototype(subClass, superClass) {
//复制一份父类的原型副本保存在变量中
var p = inheritObject(superClass.prototype)
//修正因为重写子类原型导致子类的constructor属性被修改
p.constructor = subClass
//设置子类的原型
subClass.prototype = p
}
//test
function SuperClass(current) {
//引用类型共有属性
this.courses = ["语文", "数学", "英语"];
// 值类型共有属性
this.current = current;
}
SuperClass.prototype.getCourses = function () {
console.log(this.courses);
};
SuperClass.prototype.getCurrent = function () {
console.log(this.current);
};
// 声明子类
function SubClass(current, time) {
//构造函数继承父类属性
SuperClass.call(this, current);
this.time = time;
}
//寄生式继承 子类原型继承父类
inheritPrototype(SubClass, SuperClass);
//类式继承 子类原型继承父类
// SubClass.prototype = new SuperClass();
//子类原型方法
SubClass.prototype.getTime = function () {
console.log(this.time);
};
var instance1 = new SubClass("语文", "9:00");
var instance2 = new SubClass("数学", "10:00");
instance1.getTime(); //9:00
instance1.courses.push("化学");
instance1.getCourses(); //["语文", "数学", "英语", "化学"]
instance1.getCurrent(); //语文
console.log(instance1.current); //语文
instance2.getTime(); //10:00
instance2.getCourses(); //["语文", "数学", "英语"]
instance2.getCurrent(); //数学
console.log(instance2.current); //数学
区别仅在
//寄生式继承 子类原型继承父类
inheritPrototype(SubClass, SuperClass);
//类式继承 子类原型继承父类
// SubClass.prototype = new SuperClass();
看完了这篇文章,相信你对“javascript中实现继承的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。