首页 >> js开发 >> jsJS中的继承操作实例总结js大全
jsJS中的继承操作实例总结js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
本文实例讲述了JS中的继承操作。分享给大家供大家参考,具体如下:1.原型链继承
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
ths.subproperty = false;
}
SubType.prototype = new SuperType();
// 实现继承
SubType.prototype.getSubValue = function() {
return this.subproperty;
}
var instance = new SubType();
console.log(instance.getSuperValue());
// true
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
ths.subproperty = false;
}
SubType.prototype = new SuperType();
// 实现继承
SubType.prototype.getSubValue = function() {
return this.subproperty;
}
var instance = new SubType();
console.log(instance.getSuperValue());
// true
原理:让SuperType的实例成为子类的原型对象,子类的实例拥有了父类的属性和方法。但也有弊端,如果父类的属性是引用类型,这将导致共享的属性被改变的时候,全部的属性将被改变,我们一下代码。
function SuperType() {
this.property = [1,2,3];
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
ths.subproperty = false;
}
SubType.prototype = new SuperType();
// 实现继承
SubType.prototype.getSubValue = function() {
return this.subproperty;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.property.push(4);
console.log(instance1.property);
// [1,2,3,4]
console.log(instance2.property);
// [1,2,3,4]
function SuperType() {
this.property = [1,2,3];
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
ths.subproperty = false;
}
SubType.prototype = new SuperType();
// 实现继承
SubType.prototype.getSubValue = function() {
return this.subproperty;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.property.push(4);
console.log(instance1.property);
// [1,2,3,4]
console.log(instance2.property);
// [1,2,3,4]
我们修改一处的实例属性,两个实例的属性都会被修改,因为这个属性是共享的,这也是原型链继承的缺点。2.构造函数继承
function SuperType(name) {
this.name = name;
this.numbers = [1,2,3];
}
function SubType() {
SuperType.call(this,"Nicholas");
this.age = 29;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.property.push(4);
console.log(instance1.name);
// Nicholas
console.log(instance1.age);
// 29
console.log(instance1.numbers);
// [1,2,3,4]
console.log(instance2.numbers); // [1,2,3]
function SuperType(name) {
this.name = name;
this.numbers = [1,2,3];
}
function SubType() {
SuperType.call(this,"Nicholas");
this.age = 29;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.property.push(4);
console.log(instance1.name);
// Nicholas
console.log(instance1.age);
// 29
console.log(instance1.numbers);
// [1,2,3,4]
console.log(instance2.numbers); // [1,2,3]
在子类中调用父类的构造函数,每次实例化时都会新建父类的属性放在新实例中,因此每个实例中的属性都是不一样的,改变一个实例的值不会造成另一个实例的值改变。这个继承方式的缺点是方法的定义不能复用。3.组合继承这个方法将原型链继承和构造函数继承结合到一起,融合了他们各自的优点。
function SuperType(name) {
this.name = name;
this.colors = ["red","blue","green"]
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name,age) {
SuperType.call(this,name);
this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("Nicholas", 29);
var instance2 =new SubType("Greg", 27);
instance1.colors.push("black");
console.log(instance1.colors);
// red,blue,green,black
console.log(instance2.colors);
// red,blue,green
instance1.sayName();
// Nicholas
instance2.sayName();
// Greg
instance1.sayAge();
// 29
instance2.sayAge();
// 27
function SuperType(name) {
this.name = name;
this.colors = ["red","blue","green"]
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name,age) {
SuperType.call(this,name);
this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("Nicholas", 29);
var instance2 =new SubType("Greg", 27);
instance1.colors.push("black");
console.log(instance1.colors);
// red,blue,green,black
console.log(instance2.colors);
// red,blue,green
instance1.sayName();
// Nicholas
instance2.sayName();
// Greg
instance1.sayAge();
// 29
instance2.sayAge();
// 27
4.class继承ES6中可以通过class创建对象,通过关键字extends继承。
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
this.color = color; // ReferenceError
super(x, y);
this.color = color; // 正确
}
}
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
this.color = color; // ReferenceError
super(x, y);
this.color = color; // 正确
}
}
在ES6的继承中,子类一定要重写父类的构造函授的方法,否则会报错。感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》javascript面向对象入门教程JavaScript错误与调试技巧总结JavaScript数据结构与算法技巧总结JavaScript遍历算法与技巧总结JavaScript数学运算用法总结希望本文所述对大家JavaScript程序设计有所帮助。
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
ths.subproperty = false;
}
SubType.prototype = new SuperType();
// 实现继承
SubType.prototype.getSubValue = function() {
return this.subproperty;
}
var instance = new SubType();
console.log(instance.getSuperValue());
// true
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
ths.subproperty = false;
}
SubType.prototype = new SuperType();
// 实现继承
SubType.prototype.getSubValue = function() {
return this.subproperty;
}
var instance = new SubType();
console.log(instance.getSuperValue());
// true
原理:让SuperType的实例成为子类的原型对象,子类的实例拥有了父类的属性和方法。但也有弊端,如果父类的属性是引用类型,这将导致共享的属性被改变的时候,全部的属性将被改变,我们一下代码。
function SuperType() {
this.property = [1,2,3];
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
ths.subproperty = false;
}
SubType.prototype = new SuperType();
// 实现继承
SubType.prototype.getSubValue = function() {
return this.subproperty;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.property.push(4);
console.log(instance1.property);
// [1,2,3,4]
console.log(instance2.property);
// [1,2,3,4]
function SuperType() {
this.property = [1,2,3];
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function SubType() {
ths.subproperty = false;
}
SubType.prototype = new SuperType();
// 实现继承
SubType.prototype.getSubValue = function() {
return this.subproperty;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.property.push(4);
console.log(instance1.property);
// [1,2,3,4]
console.log(instance2.property);
// [1,2,3,4]
我们修改一处的实例属性,两个实例的属性都会被修改,因为这个属性是共享的,这也是原型链继承的缺点。2.构造函数继承
function SuperType(name) {
this.name = name;
this.numbers = [1,2,3];
}
function SubType() {
SuperType.call(this,"Nicholas");
this.age = 29;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.property.push(4);
console.log(instance1.name);
// Nicholas
console.log(instance1.age);
// 29
console.log(instance1.numbers);
// [1,2,3,4]
console.log(instance2.numbers); // [1,2,3]
function SuperType(name) {
this.name = name;
this.numbers = [1,2,3];
}
function SubType() {
SuperType.call(this,"Nicholas");
this.age = 29;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.property.push(4);
console.log(instance1.name);
// Nicholas
console.log(instance1.age);
// 29
console.log(instance1.numbers);
// [1,2,3,4]
console.log(instance2.numbers); // [1,2,3]
在子类中调用父类的构造函数,每次实例化时都会新建父类的属性放在新实例中,因此每个实例中的属性都是不一样的,改变一个实例的值不会造成另一个实例的值改变。这个继承方式的缺点是方法的定义不能复用。3.组合继承这个方法将原型链继承和构造函数继承结合到一起,融合了他们各自的优点。
function SuperType(name) {
this.name = name;
this.colors = ["red","blue","green"]
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name,age) {
SuperType.call(this,name);
this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("Nicholas", 29);
var instance2 =new SubType("Greg", 27);
instance1.colors.push("black");
console.log(instance1.colors);
// red,blue,green,black
console.log(instance2.colors);
// red,blue,green
instance1.sayName();
// Nicholas
instance2.sayName();
// Greg
instance1.sayAge();
// 29
instance2.sayAge();
// 27
function SuperType(name) {
this.name = name;
this.colors = ["red","blue","green"]
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name,age) {
SuperType.call(this,name);
this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("Nicholas", 29);
var instance2 =new SubType("Greg", 27);
instance1.colors.push("black");
console.log(instance1.colors);
// red,blue,green,black
console.log(instance2.colors);
// red,blue,green
instance1.sayName();
// Nicholas
instance2.sayName();
// Greg
instance1.sayAge();
// 29
instance2.sayAge();
// 27
4.class继承ES6中可以通过class创建对象,通过关键字extends继承。
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
this.color = color; // ReferenceError
super(x, y);
this.color = color; // 正确
}
}
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
this.color = color; // ReferenceError
super(x, y);
this.color = color; // 正确
}
}
在ES6的继承中,子类一定要重写父类的构造函授的方法,否则会报错。感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》javascript面向对象入门教程JavaScript错误与调试技巧总结JavaScript数据结构与算法技巧总结JavaScript遍历算法与技巧总结JavaScript数学运算用法总结希望本文所述对大家JavaScript程序设计有所帮助。
相关文章:
- jsvue data对象重新赋值无效(未更改)的解决方式js大全
- js浅谈Vuex的this.$store.commit和在Vue项目中引用公共方法js大全
- js解决vue动态路由异步加载import组件,加载不到module的问题js大全
- jsAngular利用HTTP POST下载流文件的步骤记录js大全
- js解决vuex数据页面刷新后初始化操作js大全
- jsvue 页面回退mounted函数不执行的解决方案js大全
- jsvue项目使用$router.go(-1)返回时刷新原来的界面操作js大全
- jsElement Input输入框的使用方法js大全
- js关于angular浏览器兼容性问题的解决方案js大全
- JavaScriptthree.js欧拉角和四元数的使用方法