96SEO 2026-08-15 10:37 13
今天我把书中的 5 种继承模式结合自己这些年的实操踩坑整理出来每种都配可运行的精简代码、高频踩坑点和面试常考方向。看完不仅能搞定手写继承的面试题,日常开发选技术方法也能心里有底。话说回来,
在 Java 这类基于类的语言里继承干了两件事:
但 JS 是弱类型语言,根本不存在类型转换这回事。对 JS 的对象关键的是它能做什么而不是它从哪个类继承来的。JS 的继承,主要目标只有一个:代码复用。
而且 JS 走的是 “原型继承` 的路子 —— 对象直接从另一个对象继承属性,不需要中间夹一个 “类” 的概念。简单讲的 class 本质上只是给原型套了层大家眼熟的语法糖而已。
简单讲 “伪类”。就是 JS 为了迎合大家写类的习惯,搞出来的一套 “构造函数 + prototype” 的模拟写法。看着像 Java 的类,实则底层还是原型。
每个函数创建的时候。都会自带一个 prototype 对象,其中有个 constructor 指回函数本身。当我们用 new 调用这个函数时会生成一个新对象,这个对象会沿着构造函数的 prototype 链获取属性和方法。
实现继承。只需要把子类的 prototype 替换成父类实例,这样子类实例就能顺着原型链拿到父类的方法。话说回来,
// 父类构造函数
function Mammal {
this.name = name;}
Mammal.prototype.get_name = function {
return this.name;},Mammal.prototype.says = function {
return this.saying || '';},// 子类构造函数
function Cat {
this.name = name;this.saying = 'meow';}
// 主要:子类原型替换为父类实例
Cat.prototype = new Mammal;// 给子类
自有方法
Cat.prototype.purr = function {
let s = '';for {
s += s?'-r' : 'r',}
return s;},// 重写父方法
Cat.prototype.get_name = function {
return `${this.says} ${this.name} ${this.says}`;},// 测试
const myCat = new Cat;console.log);// 'meow'
console.log);// 'r-r-r-r-r'
console.log);// 'meow Henrietta meow'
# 优雅封装 #
Function.prototype.inherits = function {
this.prototype = new Parent;return this,};// 链式写法清爽很多
const Cat = function {
this.name = name;this.saying = 'meow';}.inherits,
new 直接污染全局: 不使用 new 调用构造函数时this 会绑定到全局对象(浏览器里是 window)。结果是悄无声息地修改全局变量,没有编译警告。也没有运行时报错——排查起来极其痛苦。约定首字母大写只是“肉眼”检测手段,并非语法保障。.call ——代码冗长且易出错。
If a constructor needs many parameters。passing m one by one is error‑prone. The solution is to pass a single configuration object where keys map to arguments,making order irrelevant and defaults easy to apply.
// 糟糕的写法:参数多了根本记不住顺序
// const myObject = maker;// 舒服的写法:传对象,可读性拉满
const myObject = maker({
first这方面。'张',last: '三',city: '北京',state: '北京',age,});
# 小结 # : 虽然大多数人已经在日常编码中使用这种方式,但很少有人意识到它源自《JS 语言精粹》章节里关于“对象说明符”的常用方法。
No constructor。no prototype juggling—just pick an existing object as prototype of a new one and tweak differences.
// 基础对象
const myMammal = {
说到name,'Herb Mammal',get_name { return this.name;老实说,},says { return this.saying || '';}
},// 基于基础对象生成新实例
const myCat = Object.create;
// 差异化补丁
myCat.name = 'Henrietta';myCat.saying = 'meow';怎么说呢,myCat.purr = function {
let s='';for s+=s,'-r':'r';return s,};myCat.get_name = function {
return `${this.says} ${this.name} ${this.says}`;},
A factory function creates an object。closes over private variables via closure,and returns an object that only exposes privileged methods.
// 父级工厂函数
function mammal {
const that = {};其实,// 特权方法
that.get_name = function { return spec.name;},that.says = function { return spec.saying || '';},return that;话说回来,}
// 子级工厂函数
function cat {
spec.saying = spec.saying || 'meow';老实说,// 调用父级工厂得到基础对象
const that = mammal;老实说,// 子级专属能力
that.purr = function {
let s='';for s+=s,'-r':'r';return s,};话说回来,that.get_name = function {
return `${that.says} ${spec.name} ${that.says}`;},return that;}
// 测试 —— 完全不需要 new
const myCat = cat;console.log);// "meow Henrietta meow"
console.log;// undefined —— 真正私有
If you need to call overridden parent method elegantly,add a generic .superior-helper:
Object.prototype.superior = function { const that = this;const method = that;return function { return method.apply;},};function coolcat{ const that = cat;const superNameFn= that.superior;that.get_name = function{ return `like ${superNameFn} baby`;},return that;} const myCoolCat= coolcat;console.log);// "like meow Bix meow baby"
Create small reusable mixin functions. Each mixin receives a target object。augments it with methods/properties,and returns same object – enabling chainable composition.
function eventuality{ const registry={};// 注册事件 that.on=function{ const handler={method,parameters};其实,registry,registry.push:registry=;return that,};// 派发事件 that.fire=function{ const type=typeof event==='string'?event:event.type;ifreturn that;registry.forEach(handler=>{ const fn=typeof handler.method==='string'?that:handler.method;fn.apply,});return that,};return that,}
The mixin can be applied to any plain object:
// 给普通对象加事件能力 const myObj= eventuality;myObj.on=>console.log);myObj.fire,// 输出 hello
The five patterns each solve a specific set of pain points. Pick one whose strengths align with your project constraints:
关键提醒
This final text should be well-formatted without extra explanations beyond HTML content as requested.
。作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback