当前位置:网站首页>那些易混淆的概念(三):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指向
边栏推荐
- 学习记录——高精度加法和乘法
- Fiddler break point
- MCU is the most popular science (ten thousand words summary, worth collecting)
- [STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program
- php \n 换行无法输出
- How embedded engineers improve work efficiency
- 根据设备信息进行页面跳转至移动端页面或者PC端页面
- MySQL insert data create trigger fill UUID field value
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- Some superficial understanding of word2vec
猜你喜欢
电表远程抄表拉合闸操作命令指令
Programming features of ISP, IAP, ICP, JTAG and SWD
Leetcode-560: subarray with sum K
mysql插入数据创建触发器填充uuid字段值
1324:【例6.6】整数区间
Leetcode-304: two dimensional area and retrieval - matrix immutable
Guide de signature du Code Appx
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
Several schemes of building hardware communication technology of Internet of things
Pdf document signature Guide
随机推荐
JMeter loop controller and CSV data file settings are used together
2022.7.3DAY595
移动端通过设置rem使页面内容及字体大小自动调整
Leetcode exercise - 113 Path sum II
电表远程抄表拉合闸操作命令指令
Review of the losers in the postgraduate entrance examination
Adb 实用命令(网络包、日志、调优相关)
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
Serial communication relay Modbus communication host computer debugging software tool project development case
Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
EasyExcel读取写入简单使用
XML configuration file parsing and modeling
Why is the reflection efficiency low?
Smart city construction based on GIS 3D visualization technology
Jump to the mobile terminal page or PC terminal page according to the device information
施努卡:机器人视觉抓取工作原理 机器视觉抓取
When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
Programming features of ISP, IAP, ICP, JTAG and SWD
JMeter installation
IO model review