当前位置:网站首页>那些易混淆的概念(三):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指向
边栏推荐
- 根据设备信息进行页面跳转至移动端页面或者PC端页面
- 【剑指Offer】42. 栈的压入、弹出序列
- Prototype and prototype chain
- Fiddler simulates the interface test
- Differences between MCU and MPU
- JMeter about setting thread group and time
- Guide de signature du Code Appx
- Study summary of postgraduate entrance examination in September
- . Net configuration system
- gym安装踩坑记录
猜你喜欢

Leetcode-560: subarray with sum K

01 use function to approximate cosine function (15 points)

Embedded background - chip

对存储过程进行加密和解密(SQL 2008/SQL 2012)

leetcode-560:和为 K 的子数组

1321:【例6.3】删数问题(Noip1994)

Inno setup packaging and signing Guide
![[second on] [jeecgboot] modify paging parameters](/img/59/55313e3e0cf6a1f7f6b03665e77789.png)
[second on] [jeecgboot] modify paging parameters

IO模型复习

基于gis三维可视化技术的智慧城市建设
随机推荐
IIC Basics
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
table宽度比tbody宽度大4px
Factorial implementation of large integer classes
IPv4 socket address structure
IPv4套接字地址结构
2022.7.4DAY596
[email protected] can help us get the log object quickly
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
Jump to the mobile terminal page or PC terminal page according to the device information
优雅的 Controller 层代码
HDU-2196 树形DP学习笔记
Trajectory planning for multi robot systems: methods and Applications Overview reading notes
C logging method
Appx代码签名指南
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
I'd rather say simple problems a hundred times than do complex problems once
Sword finger offer 38 Arrangement of strings [no description written]
搭建物联网硬件通信技术几种方案