当前位置:网站首页>那些易混淆的概念(三):function和class
那些易混淆的概念(三):function和class
2022-07-07 08:26:00 【xinxin_csdn】
ES6中的class类(oop面向对象的编程思想)和function区别
相同点:
都可作为构造函数,通过new操作符来实例化
函数实现构造函数
function Person(name) {
this.name = name
}
const user = new Person('Jack')
console.log(user); // Person { name: 'Jack' }类实现构造函数,其中的constructor方法是一个特殊的方法,称为构造函数方法;
用new创建实例后,自动调用constructor,不定义constructor,相当于constructor是空函数
class Person {
constructor(name) {
this.name = name
}
}
const user = new Person('Jack')
console.log(user); // Person { name: 'Jack' } 不同点
1、调用类构造函数必须使用new操作符, 而普通的function构造函数如果不使用new,就会以全局的this,作为内部对象
2、function构造函数声明存在变量提升,可以先使用, class声明不存在变量提升,实例化对象必须写在声明之后
const user = new Person('jack')
function Person(name) {
this.name = name
}3、class不可以使用call、apply、bind改变this指向
边栏推荐
- Experience sharing of software designers preparing for exams
- Study summary of postgraduate entrance examination in October
- Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
- IO model review
- gym安装踩坑记录
- 深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
- Kotlin realizes wechat interface switching (fragment exercise)
- Slurm资源管理与作业调度系统安装配置
- 1324:【例6.6】整数区间
- 【剑指Offer】42. 栈的压入、弹出序列
猜你喜欢

5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!

基于gis三维可视化技术的智慧城市建设

LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件

Talking about the return format in the log, encapsulation format handling, exception handling

This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system

Fiddler break point

High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors

【acwing】786. 第k个数

Remote meter reading, switching on and off operation command

Appx代碼簽名指南
随机推荐
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Use of JSON extractor originals in JMeter
搭建物联网硬件通信技术几种方案
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Hdu-2196 tree DP learning notes
Easyexcel read write simple to use
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
BigDecimal value comparison
【作业】2022.7.6 写一个自己的cal函数
Programming features of ISP, IAP, ICP, JTAG and SWD
PDF文档签名指南
根据设备信息进行页面跳转至移动端页面或者PC端页面
leetcode-303:区域和检索 - 数组不可变
A small problem of bit field and symbol expansion
对word2vec的一些浅层理解
浅谈日志中的返回格式封装格式处理,异常处理
P2788 数学1(math1)- 加减算式
Kotlin实现微信界面切换(Fragment练习)
Jump to the mobile terminal page or PC terminal page according to the device information
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.