当前位置:网站首页>那些易混淆的概念(三):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指向
边栏推荐
- Learning records - high precision addition and multiplication
- Talking about the return format in the log, encapsulation format handling, exception handling
- Embedded background - chip
- Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
- 【二开】【JeecgBoot】修改分页参数
- [homework] 2022.7.6 write your own cal function
- Postman interface test VI
- Differences between MCU and MPU
- . Net configuration system
- Experience sharing of software designers preparing for exams
猜你喜欢
![[sword finger offer] 42 Stack push in and pop-up sequence](/img/f4/eb69981163683c5b36f17992a87b3e.png)
[sword finger offer] 42 Stack push in and pop-up sequence

字符串格式化

Several schemes of building hardware communication technology of Internet of things

How to cancel automatic saving of changes in sqlyog database

Use the fetch statement to obtain the repetition of the last row of cursor data

Application of OpenGL gllightfv function and related knowledge of light source

浅谈日志中的返回格式封装格式处理,异常处理

STM32 Basics - memory mapping
![[second on] [jeecgboot] modify paging parameters](/img/59/55313e3e0cf6a1f7f6b03665e77789.png)
[second on] [jeecgboot] modify paging parameters

fiddler-AutoResponder
随机推荐
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
Postman interface test VI
IPv4 socket address structure
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
Talking about the return format in the log, encapsulation format handling, exception handling
[email protected]能帮助我们快速拿到日志对象
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
IDA中常见快捷键
Prototype and prototype chain
Postman interface test VII
The width of table is 4PX larger than that of tbody
555电路详解
High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
Differences between MCU and MPU
0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
Socket通信原理和实践
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
Hdu-2196 tree DP learning notes
Yarn的基础介绍以及job的提交流程
反射效率为什么低?