当前位置:网站首页>Those confusing concepts (3): function and class
Those confusing concepts (3): function and class
2022-07-07 10:38:00 【xinxin_ csdn】
ES6 Medium class class (oop Object-oriented programming ideas ) and function difference
The same thing :
Can be used as a constructor , adopt new Operator to instantiate
Function implementation constructor
function Person(name) {
this.name = name
}
const user = new Person('Jack')
console.log(user); // Person { name: 'Jack' }
Constructor class implementation , Among them constructor Method is a special method , Called constructor method ;
use new After creating the instance , Automatically call constructor, Does not define constructor, amount to constructor It's a null function
class Person {
constructor(name) {
this.name = name
}
}
const user = new Person('Jack')
console.log(user); // Person { name: 'Jack' }
Difference
1、 Calling a class constructor must use new The operator , And ordinary. function Constructor if not used new, Will take the overall situation this, As internal objects
2、function Constructor declaration has variable promotion , You can use it first , class Declare that there is no variable promotion , The instantiated object must be written after the declaration
const user = new Person('jack')
function Person(name) {
this.name = name
}
3、class Not available call、apply、bind change this Point to
边栏推荐
- 成为优秀的TS体操高手 之 TS 类型体操前置知识储备
- Study summary of postgraduate entrance examination in July
- 施努卡:机器视觉定位技术 机器视觉定位原理
- 2022年7月10日“五心公益”活动通知+报名入口(二维码)
- @Configuration, use, principle and precautions of transmission:
- 【实战】霸榜各大医学分割挑战赛的Transformer架构--nnFormer
- 【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
- Gym installation pit records
- 深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
- Experience sharing of software designers preparing for exams
猜你喜欢
求最大公约数与最小公倍数(C语言)
555 circuit details
P1031 [noip2002 improvement group] average Solitaire
I plan to take part in security work. How about information security engineers and how to prepare for the soft exam?
leetcode-304:二维区域和检索 - 矩阵不可变
中级软件评测师考什么
1324:【例6.6】整数区间
CAS mechanism
How to successfully pass the senior system architecture designer in the second half of the year?
How to prepare for the advanced soft test (network planning designer)?
随机推荐
【推薦系統 01】Rechub
ArrayList线程不安全和解决方案
P1031 [NOIP2002 提高组] 均分纸牌
IIC基本知识
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Leetcode-304: two dimensional area and retrieval - matrix immutable
CAS mechanism
P2788 math 1 - addition and subtraction
那些易混淆的概念(三):function和class
Openinstall and Hupu have reached a cooperation to mine the data value of sports culture industry
2022.7.3DAY595
Elegant controller layer code
MySQL insert data create trigger fill UUID field value
About hzero resource error (groovy.lang.missingpropertyexception: no such property: weight for class)
长列表性能优化方案 memo
【作业】2022.7.6 写一个自己的cal函数
【推荐系统 01】Rechub
leetcode-560:和为 K 的子数组
无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。
2022年7月10日“五心公益”活动通知+报名入口(二维码)