Jacky's blog Jacky's blog
首页
  • 编码专题
  • 深入浅出 Vite
  • 深入浅出 babel
  • 快速上手API
  • 深入浅出 react
  • Node

    • code-notebook
  • 状态管理

    • redux
  • 前端工程化

    • Wepack
  • React源码

    • React源码
  • 组件库封装

    • 组件库
  • 开发工具

    • Vscode 插件
  • 项目展示
  • 案例中心 (opens new window)
  • First Project
  • 基础算法题
  • 链表题
  • 动态规划
  • 双指针
  • 递归
  • 数据结构
  • 前端学习计划 (opens new window)
  • 技术随笔
  • 转载文章
  • 包管理工具
  • 前端学习周报
  • VSCode插件
  • Promise 专题
  • 函数技巧
  • React 专题
  • 配置文件

    • TSCONFIG-配置 (opens new window)
    • NGINX-配置 (opens new window)
    • 正则规则查询手册 (opens new window)
    • Lint 配置 (opens new window)
  • 教程

    • GIT-教程
    • NPM SCRIPTS-工作流 (opens new window)
    • DOCKER-教程 (opens new window)
    • LERNA-教程 (opens new window)
    • GIT-常用操作整理 (opens new window)
  • VSCode

    • LAUNCH.JSON (opens new window)
  • 指令

    • NPM 指令 (opens new window)
    • NVM 指令 (opens new window)
    • Nginx 指令 (opens new window)
    • YARN 指令 (opens new window)
    • PNPM 指令 (opens new window)
  • 库

    • FS-EXTRA 库 (opens new window)
    • NODE 库-PATH (opens new window)
  • 永远的神

    • 魔法师卡颂-自顶向下学 React 源码 (opens new window)
    • 全栈潇晨 (opens new window)
    • 博客-程序员山月-Daily (opens new window)
    • 淘系前端:冴羽 (opens new window)
  • 系列文章

    • 《图解HTTP》 (opens new window)
    • 《ES6标准入门》 (opens new window)
    • 《现代JavaScript教程》 (opens new window)
    • 《深入浅出Webpack》 (opens new window)
    • VSCode 插件系列:小茗同学 (opens new window)
    • JEST 教程 (opens new window)
    • 前端精读周刊:各种精读系列 (opens new window)
    • 一文吃透系列 (opens new window)
    • 图解 REACT 原理 (opens new window)
  • 实用网站

    • MDN (opens new window)
    • CAN I USE (opens new window)
    • TYPESCRIPT-ESLint-RULES (opens new window)
    • ESLint-RULES (opens new window)
    • FRONT-END TREND (opens new window)
    • NPM TREND (opens new window)
    • 在线分析 Node 依赖 (opens new window)
    • FIND NPM (opens new window)
    • CODE PEN (opens new window)
    • 印记中文 (opens new window)
    • TOOL.LU (opens new window)
    • 阮一峰-网道 (opens new window)
    • DIGITAL OCEAN (opens new window)
    • DEVDOCS.IO (opens new window)
    • JOI (opens new window)
  • 算法

    • 小浩算法 (opens new window)
    • LABULADONG 的算法小抄 (opens new window)
    • 力扣 SOLUTION (opens new window)
    • HACKER RANK (opens new window)
    • 代码随想录 (opens new window)
  • 博客系列

    • 美团大佬 (opens new window)
    • 蜡笔小伟 (opens new window)
    • 优秀博客1 (opens new window)
    • 优秀博客2-umi (opens new window)
    • 优质博客 (opens new window)
  • CSS

    • CSS-EASING 库 (opens new window)
    • ROUGH.JS (opens new window)
    • CSS 网站收集
    • UNOCSS (opens new window)
  • 前端

    • PROMISE (opens new window)
    • UNDERSCORE.JS (opens new window)
    • study with BGM (opens new window)
    • nginx【B站视频】 (opens new window)
    • 机器学习
    • Js基础
  • 掘金已购课程

    • 前端自动化测试精讲 (opens new window)
    • 深入浅出 Vite (opens new window)
    • 现代 Web 布局 (opens new window)
    • 前端算法与数据结构 (opens new window)
    • 基于 Vite 的 SSG 框架开发实战 (opens new window)
    • SSR 实战:官网开发指南 (opens new window)
    • WebGL 入门与实践 (opens new window)
    • 玩转 CSS 的艺术之美 (opens new window)
    • 前端调试通关秘籍 (opens new window)
    • React 进阶实践指南 (opens new window)
    • TypeScript 全面进阶指南 (opens new window)
    • 前端缓存技术与方案解析 (opens new window)
    • npm scripts 前端工作流 (opens new window)
    • Webpack5 核心原理与应用实践 (opens new window)
  • 购物车

    • 张鑫旭-技术写作指南 (opens new window)
    • 深入剖析 Node.js 底层原理 (opens new window)
    • 前端开发者的现代 C++ 课 (opens new window)
    • 从前端到全栈 (opens new window)
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Jacky Wang

行到水穷处,坐看云起时
首页
  • 编码专题
  • 深入浅出 Vite
  • 深入浅出 babel
  • 快速上手API
  • 深入浅出 react
  • Node

    • code-notebook
  • 状态管理

    • redux
  • 前端工程化

    • Wepack
  • React源码

    • React源码
  • 组件库封装

    • 组件库
  • 开发工具

    • Vscode 插件
  • 项目展示
  • 案例中心 (opens new window)
  • First Project
  • 基础算法题
  • 链表题
  • 动态规划
  • 双指针
  • 递归
  • 数据结构
  • 前端学习计划 (opens new window)
  • 技术随笔
  • 转载文章
  • 包管理工具
  • 前端学习周报
  • VSCode插件
  • Promise 专题
  • 函数技巧
  • React 专题
  • 配置文件

    • TSCONFIG-配置 (opens new window)
    • NGINX-配置 (opens new window)
    • 正则规则查询手册 (opens new window)
    • Lint 配置 (opens new window)
  • 教程

    • GIT-教程
    • NPM SCRIPTS-工作流 (opens new window)
    • DOCKER-教程 (opens new window)
    • LERNA-教程 (opens new window)
    • GIT-常用操作整理 (opens new window)
  • VSCode

    • LAUNCH.JSON (opens new window)
  • 指令

    • NPM 指令 (opens new window)
    • NVM 指令 (opens new window)
    • Nginx 指令 (opens new window)
    • YARN 指令 (opens new window)
    • PNPM 指令 (opens new window)
  • 库

    • FS-EXTRA 库 (opens new window)
    • NODE 库-PATH (opens new window)
  • 永远的神

    • 魔法师卡颂-自顶向下学 React 源码 (opens new window)
    • 全栈潇晨 (opens new window)
    • 博客-程序员山月-Daily (opens new window)
    • 淘系前端:冴羽 (opens new window)
  • 系列文章

    • 《图解HTTP》 (opens new window)
    • 《ES6标准入门》 (opens new window)
    • 《现代JavaScript教程》 (opens new window)
    • 《深入浅出Webpack》 (opens new window)
    • VSCode 插件系列:小茗同学 (opens new window)
    • JEST 教程 (opens new window)
    • 前端精读周刊:各种精读系列 (opens new window)
    • 一文吃透系列 (opens new window)
    • 图解 REACT 原理 (opens new window)
  • 实用网站

    • MDN (opens new window)
    • CAN I USE (opens new window)
    • TYPESCRIPT-ESLint-RULES (opens new window)
    • ESLint-RULES (opens new window)
    • FRONT-END TREND (opens new window)
    • NPM TREND (opens new window)
    • 在线分析 Node 依赖 (opens new window)
    • FIND NPM (opens new window)
    • CODE PEN (opens new window)
    • 印记中文 (opens new window)
    • TOOL.LU (opens new window)
    • 阮一峰-网道 (opens new window)
    • DIGITAL OCEAN (opens new window)
    • DEVDOCS.IO (opens new window)
    • JOI (opens new window)
  • 算法

    • 小浩算法 (opens new window)
    • LABULADONG 的算法小抄 (opens new window)
    • 力扣 SOLUTION (opens new window)
    • HACKER RANK (opens new window)
    • 代码随想录 (opens new window)
  • 博客系列

    • 美团大佬 (opens new window)
    • 蜡笔小伟 (opens new window)
    • 优秀博客1 (opens new window)
    • 优秀博客2-umi (opens new window)
    • 优质博客 (opens new window)
  • CSS

    • CSS-EASING 库 (opens new window)
    • ROUGH.JS (opens new window)
    • CSS 网站收集
    • UNOCSS (opens new window)
  • 前端

    • PROMISE (opens new window)
    • UNDERSCORE.JS (opens new window)
    • study with BGM (opens new window)
    • nginx【B站视频】 (opens new window)
    • 机器学习
    • Js基础
  • 掘金已购课程

    • 前端自动化测试精讲 (opens new window)
    • 深入浅出 Vite (opens new window)
    • 现代 Web 布局 (opens new window)
    • 前端算法与数据结构 (opens new window)
    • 基于 Vite 的 SSG 框架开发实战 (opens new window)
    • SSR 实战:官网开发指南 (opens new window)
    • WebGL 入门与实践 (opens new window)
    • 玩转 CSS 的艺术之美 (opens new window)
    • 前端调试通关秘籍 (opens new window)
    • React 进阶实践指南 (opens new window)
    • TypeScript 全面进阶指南 (opens new window)
    • 前端缓存技术与方案解析 (opens new window)
    • npm scripts 前端工作流 (opens new window)
    • Webpack5 核心原理与应用实践 (opens new window)
  • 购物车

    • 张鑫旭-技术写作指南 (opens new window)
    • 深入剖析 Node.js 底层原理 (opens new window)
    • 前端开发者的现代 C++ 课 (opens new window)
    • 从前端到全栈 (opens new window)
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 常用资源导航
  • 概念
  • 代码格式化
  • 面向对象基础
    • 1.蛋炒饭与盖浇饭
    • 2.基础介绍
      • 2.1 创建class
      • 2.2 构造函数constructor
      • 2.3 class中添加function
    • 3.继承
      • 3.1 继承extends
      • 3.2 关键词super
    • 4.this的说明
    • 规范注意
    • 规范
  • 构造函数与原型
  • 函数进阶
  • 函数柯里化
  • 函数-传参
  • axios
  • Node 和 ES6 模块系统
  • JS
wangjiasheng
2021-01-07
目录

面向对象基础

# 1.蛋炒饭与盖浇饭

编程思想可由两大类组成:

  • 面向过程:POP【Process-oriented programming】
    • 优点:性能相较于面向对象高
    • 缺点:!(OOP的优点)
  • 面向对象:OOP【Object Oriented Programming】
    • 优点:易维护、复用、扩展,可以设计出低耦合的系统。
    • 缺点:!(POP的优点)

用面向过程的方法写出来的程序是一份蛋炒饭,而用面向对象写出来的程序是一份盖浇饭。

# 2.基础介绍

面向对象需要掌握以下三个特性:

  1. 封装性
  2. 继承性
  3. 多态性

# 2.1 创建class

类由:constructor函数 + 其余函数构成

class FunctionName{
    constructor(uname){
        this.uname = uname;
    }
    method1(){
        .................;
    }
    method2(){
        .................;
    }
}

// 调用时:new关键字
var ldh = new FunctionName("刘德华");
1
2
3
4
5
6
7
8
9
10
11
12
13
14

⚠️注意:

  • 类必须使用new来实例化对象,new会自动调用constructor函数。

  • 常见的错误:

    class Person(){};// 错
    class Person{};// 不加括号,才是正解
    
    1
    2

# 2.2 构造函数constructor

由new生成实例时,会自动调用该构造函数,如果没有构造函数,类也会自动生成这个函数。

constructor(x,y){
    super(x,y);// 子类,可以通过super关键字调用父类的constructor
    this.x = x;// 1.这里可以对数据进行集中管理
    this.y = y;
    this.say();// 2.可以调用函数
}
1
2
3
4
5
6

# 2.3 class中添加function

class FunctionName{
    constructor(){
        // 调用method
        this.method1();
        this.method2();
        this.method3();
        this.method4();
    }
    method1(参数){....}
    method2(参数){....}
    method3(参数){....}
    method4(参数){....}
}
1
2
3
4
5
6
7
8
9
10
11
12
13

⚠️注意:

class FunctionName{
    constructor(){....}, // 错误写法1:加逗号
    method1(参数){....},
    function method2(参数){....} // 错误写法2:function不需要写
}
1
2
3
4
5

# 3.继承

需要掌握的关键字:extends、super

# 3.1 继承extends

语法:

class Father{....}
class Son extends Father{....} // 子承父业
1
2

# 3.2 关键词super

子类的构造(继承)只需要加一个关键字extends就可以,其余部分的设置和正常父类的类构造方式没有什么区别,其中仍需要记忆的知识点:

  • 子类是无法拿到父类的数据,但可以使用super可以让子类重新执行一遍的父类的constructor函数,从而间接拿到父类的数据,若父元素constructor需要接受参数,super则也需要传入响应的参数

    class Person {   // 父类       
        constructor(surname){          
            this.surname = surname;      
        } 
    }  
    class  Student extends Person {       
        // 子类继承父类      
        constructor(surname,firstname){
            // 调用父类的constructor(surname)  
            super(surname);
            // 定义子类独有的属性    
            this.firstname = firstname;  
        } 
    }        
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
  • 当然super也可以调用父类的其余的method函数

    class Father {      
        say() {          
            return '我是爸爸';       
        } 
    } 
    class Son extends Father { 
        // 这样子类就继承了父类的属性和方法      
        say() {           
            // super.say()  
            super; //默认调用constructor函数,但此Demo没有,但也可以通过super调用其余的函数        
            return super.say() + '的儿子';      
        } 
    } 
    var damao = new Son(); 
    console.log(damao.say());        
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
  • super必须写子类constructor的开头

  • 若父类和子类拥有相同的function:优先执行子类中的function

# 4.this的说明

因为类是一个抽象的概念,它不具有具体的含义,只有创建后对象才有具体的内存地址,而此时我们使用this来指向这个对象具体的内存地址,故具体值的获取都必须使用this来寻找到某个具体对象的下的某个具体的值。

⚠️注意1:this中存的是触发对象的内存地址

class FunctionName{
    constructor(x,y){
        this.x = x;//当new完一个具体的对象后,会将对象的指针存在this中
        this.y = y;
    }
}
1
2
3
4
5
6

⚠️注意2:method访问constructor中的内容

var that; // that 定义为全局变量
class FunctionName{
    constructor(x,y){
        that = this; //缓存this指针,当前this一定指向Object
        this.x = x;//当new完一个具体的对象后,会将对象的指针存在this中
        this.y = y;
        this.method1();
    }
    method1(){
        // 1.一般情况下,我们直接通过this找到具体对象的存放地址,再索引value
        console.log(this.x);
        // 2.this有的时候不一定指向object的内存地址,所以可以先通过that缓存内存的地址,使用that访问对象的value
        console.log(that.x);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

⚠️注意3:触发对象的内存地址解析【面试常考题,考察this相关知识点】

const o1 = {
    text: 'o1',
    fn: function() {
        return this.text
    }
}
const o2 = {
    text: 'o2',
    fn: function() {
        return o1.fn()
    }
}
const o3 = {
    text: 'o3',
    fn: function() {
        var fn = o1.fn
        return fn()
    }
}

console.log(o1.fn());// o1
console.log(o2.fn());// o1
console.log(o3.fn());// undefined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

分析:

  • 第一个 console 最简单,o1 没有问题。难点在第二个和第三个上面,关键还是看调用 this 的那个函数。

  • 第二个 console 的 o2.fn(),最终还是调用 o1.fn(),因此答案仍然是 o1。

    ​ 🌟this 所在位置取决于最后一个()

  • 为了更好说明,详细绘制了第三个对象,各种情况下的this,实际上最终的value,只与最后的fn()有关,而fn又是新创建的变量[新的内存地址],而此时this底下并无text属性,故为undefined

    this的值分两种情况讨论:一种是正常情况下读取调用的。还有一种是触发下调用的。

    • 正常情况下,this会返回上一层的内存地址,可以通过console.log(this)查看。

    • 触发下调用的this,只需要关心最后返回值时的触发对象即可(最后一个调用的对象内存)。

# 规范注意

# 规范

  • 类名:首字母大写
编辑 (opens new window)
上次更新: 2022/04/06, 15:04:00
代码格式化
构造函数与原型

← 代码格式化 构造函数与原型→

最近更新
01
如何理解浏览器的 user agent 用户代理的含义?
11-05
02
浏览器事件循环机制
10-31
03
浏览器页面渲染机制【2023】
10-15
更多文章>
Theme by Vdoing | Copyright © 2020-2023
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式