当前位置:网站首页>那些易混淆的概念(三):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指向
边栏推荐
- Vs code specifies the extension installation location
- Jump to the mobile terminal page or PC terminal page according to the device information
- 【acwing】786. 第k个数
- Multisim--软件相关使用技巧
- IPv4套接字地址结构
- Remote meter reading, switching on and off operation command
- MCU is the most popular science (ten thousand words summary, worth collecting)
- Download Text, pictures and ab packages used by unitywebrequest Foundation
- leetcode-303:区域和检索 - 数组不可变
- 【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
猜你喜欢
Inno Setup 打包及签名指南
[daiy5] jz77 print binary tree in zigzag order
fiddler-AutoResponder
浅谈日志中的返回格式封装格式处理,异常处理
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
JMeter loop controller and CSV data file settings are used together
Use the fetch statement to obtain the repetition of the last row of cursor data
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
mysql插入数据创建触发器填充uuid字段值
【acwing】789. 数的范围(二分基础)
随机推荐
Pdf document signature Guide
Study summary of postgraduate entrance examination in August
使用U2-Net深层网络实现——证件照生成程序
PDF文档签名指南
优雅的 Controller 层代码
@Transcation的配置,使用,原理注意事项:
STM32 ADC and DMA
搭建物联网硬件通信技术几种方案
.NET配置系统
XML configuration file parsing and modeling
Study summary of postgraduate entrance examination in October
Appx代码签名指南
[second on] [jeecgboot] modify paging parameters
php \n 换行无法输出
Embedded background - chip
BigDecimal value comparison
JMeter about setting thread group and time
Serial communication relay Modbus communication host computer debugging software tool project development case
STM32 Basics - memory mapping
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file