当前位置: 首页 > wzjs >正文

做网站发房源综合语录seo公司推广宣传

做网站发房源综合语录,seo公司推广宣传,郑州有做网站的公司没,如何在本地搭建网站JavaScript 实现继承及 Class 本质详解 一、实现继承的 6 种方式 JavaScript 的继承基于原型链,以下是常见的继承实现方法及其优缺点: 1. 原型链继承 核心:子类的原型指向父类的实例。 示例: function Parent() {this.name Pa…

JavaScript 实现继承及 Class 本质详解


一、实现继承的 6 种方式

JavaScript 的继承基于原型链,以下是常见的继承实现方法及其优缺点:


1. 原型链继承

核心:子类的原型指向父类的实例。
示例

function Parent() {this.name = 'Parent';
}
Parent.prototype.say = function() {console.log(this.name);
};function Child() {}
Child.prototype = new Parent(); // 原型链继承const child = new Child();
child.say(); // 输出: "Parent"

缺点

  • 所有子类实例共享父类引用类型属性(如数组、对象)。
  • 无法向父类构造函数传参。

2. 构造函数继承

核心:在子类构造函数中调用父类构造函数。
示例

function Parent(name) {this.name = name;this.colors = ['red', 'blue'];
}function Child(name) {Parent.call(this, name); // 构造函数继承
}const child1 = new Child('Child1');
child1.colors.push('green');
console.log(child1.colors); // ["red", "blue", "green"]const child2 = new Child('Child2');
console.log(child2.colors); // ["red", "blue"]

优点

  • 解决引用类型共享问题。
  • 支持向父类传参。
    缺点
  • 无法继承父类原型上的方法(如 Parent.prototype.say)。

3. 组合继承(原型链 + 构造函数)

核心:结合原型链继承和构造函数继承。
示例

function Parent(name) {this.name = name;this.colors = ['red'];
}
Parent.prototype.say = function() {console.log(this.name);
};function Child(name) {Parent.call(this, name); // 构造函数继承(第二次调用 Parent)
}
Child.prototype = new Parent(); // 原型链继承(第一次调用 Parent)
Child.prototype.constructor = Child;const child = new Child('Child');
child.say(); // 输出: "Child"

缺点

  • 父类构造函数被调用两次(性能问题)。

4. 原型式继承

核心:基于现有对象创建新对象(类似 Object.create)。
示例

function createObject(obj) {function F() {}F.prototype = obj;return new F();
}const parent = { name: 'Parent' };
const child = createObject(parent);
console.log(child.name); // 输出: "Parent"

应用场景

  • 适合不需要构造函数的简单对象继承。

5. 寄生式继承

核心:在原型式继承基础上增强对象。
示例

function createEnhancedObject(obj) {const clone = Object.create(obj);clone.say = function() {console.log(this.name);};return clone;
}const parent = { name: 'Parent' };
const child = createEnhancedObject(parent);
child.say(); // 输出: "Parent"

缺点

  • 方法无法复用(每个实例都创建新方法)。

6. 寄生组合继承(最优方案)

核心:避免组合继承中重复调用父类构造函数。
示例

function inheritPrototype(Child, Parent) {const prototype = Object.create(Parent.prototype); // 创建父类原型的副本prototype.constructor = Child; // 修复 constructorChild.prototype = prototype; // 赋值给子类原型
}function Parent(name) {this.name = name;
}
Parent.prototype.say = function() {console.log(this.name);
};function Child(name) {Parent.call(this, name); // 构造函数继承
}inheritPrototype(Child, Parent); // 寄生组合继承const child = new Child('Child');
child.say(); // 输出: "Child"

优点

  • 只调用一次父类构造函数。
  • 原型链保持干净,无多余属性。

二、ES6 Class 的本质

ES6 的 class 是语法糖,底层仍基于原型链和构造函数。


1. Class 转换为 ES5 代码

示例

// ES6 Class
class Parent {constructor(name) {this.name = name;}say() {console.log(this.name);}static staticMethod() {console.log('Static');}
}// 转换为 ES5
function Parent(name) {this.name = name;
}
Parent.prototype.say = function() {console.log(this.name);
};
Parent.staticMethod = function() {console.log('Static');
};

核心规则

  • constructor 对应构造函数。
  • 类方法绑定到原型(Parent.prototype)。
  • 静态方法绑定到构造函数本身(Parent.staticMethod)。

2. 继承的实现

示例

class Child extends Parent {constructor(name, age) {super(name); // 调用父类构造函数this.age = age;}run() {console.log(`${this.name} is running`);}
}// 转换为 ES5
function Child(name, age) {Parent.call(this, name); // 构造函数继承this.age = age;
}
Child.prototype = Object.create(Parent.prototype); // 原型链继承
Child.prototype.constructor = Child;
Child.prototype.run = function() {console.log(this.name + ' is running');
};

关键点

  • super 在构造函数中调用父类构造函数(等价于 Parent.call(this))。
  • extends 自动设置原型链(Child.prototype.__proto__ = Parent.prototype)。

3. 静态方法与继承

示例

class Parent {static staticMethod() {console.log('Parent static');}
}class Child extends Parent {static childStaticMethod() {super.staticMethod(); // 调用父类静态方法console.log('Child static');}
}Child.childStaticMethod(); // 输出: "Parent static" → "Child static"

底层原理

  • 静态方法继承通过 Child.__proto__ = Parent 实现。

三、核心知识点总结
特性说明
原型链对象通过 __proto__ 向上查找属性和方法,形成链式结构。
构造函数用于初始化实例属性,通过 new 调用。
Class 本质语法糖,基于原型链和构造函数,提供更清晰的面向对象语法。
super在子类构造函数中调用父类构造函数,或在方法中调用父类方法。
静态方法通过 static 定义,绑定到类本身而非实例。
最优继承方案寄生组合继承(ES5)或直接使用 class + extends(ES6)。

四、总结
  • ES5 继承:需手动处理原型链(如寄生组合继承)。
  • ES6 Class:简化继承语法,但需理解其背后的原型机制。
  • 最佳实践:现代项目优先使用 classextends,必要时结合原型操作优化。
http://www.dtcms.com/wzjs/19178.html

相关文章:

  • 点评类网站建设最近几天的新闻
  • 深圳市住房和建设局网站住房淘宝运营培训
  • 公司网站横幅如何做谷歌网页版入口在线
  • 17网站一起做网店官网seo的基本步骤是什么
  • 有了云服务器怎么做网站seo点击软件
  • 内江市网站建设培训ciliba磁力搜索引擎
  • 996建站网站建设百度平台我的订单
  • 网上做预算的网站免费制作logo的网站
  • 壹佰网站建设网络服务费计入什么科目
  • 网站首页排名突然没了seo软件资源
  • wordpress评论改成微博东莞seo推广
  • 网站维护页面怎么做的如何免费推广网站
  • ps专门做兼职的网站一个新产品怎么推广
  • 把里面的dede和plugins这2个文件夹覆盖到你的网站根目录湖南网站营销seo多少费用
  • 有哪些网站有做网页用的小图片什么优化
  • 加速乐 wordpressseo搜索排名优化是什么意思
  • 做网站 站内搜索引擎数据分析师一般一个月多少钱
  • 台州关键词排名优化企业网站优化推广
  • 潮州建设局网站长沙做网站推广公司咨询
  • c2c电子商务网站定制开发营销策划公司 品牌策划公司
  • 做游戏装备网站可以吗惠州关键词排名优化
  • 购物网站开发简介如何制作网站
  • 天津旅游网站建设百度域名收录
  • 360企业自助建站手机百度ai入口
  • 深圳专业做网站设计公司网络管理系统
  • 网站建设报告书范文百度网站推广怎么做
  • 淘宝有做网站吗网站seo技术教程
  • 上海企业一网通办网站如何做优化排名
  • 重庆公安网站备案山东网站seo
  • 手机网站制作教程视频教程目前搜索引擎排名