当前位置:网站首页>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
边栏推荐
- 优雅的 Controller 层代码
- 施努卡:机器视觉定位技术 机器视觉定位原理
- 对word2vec的一些浅层理解
- Cluster task scheduling system lsf/sge/slurm/pbs based on HPC scenario
- Schnuka: machine vision positioning technology machine vision positioning principle
- Find the root of equation ax^2+bx+c=0 (C language)
- Elegant controller layer code
- leetcode-304:二维区域和检索 - 矩阵不可变
- MONAI版本更新到 0.9 啦,看看有什么新功能
- 成为优秀的TS体操高手 之 TS 类型体操前置知识储备
猜你喜欢

Socket communication principle and Practice

Yarn的基础介绍以及job的提交流程

求方程ax^2+bx+c=0的根(C语言)

【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法

Deeply analyze the main contents of erc-4907 agreement and think about the significance of this agreement to NFT market liquidity!

求最大公约数与最小公倍数(C语言)

原型与原型链

How much review time does it usually take to take the intermediate soft exam?

555 circuit details

Basic introduction of yarn and job submission process
随机推荐
Sword finger offer 38 Arrangement of strings [no description written]
String formatting
Socket communication principle and Practice
OpenGL glLightfv 函数的应用以及光源的相关知识
Study summary of postgraduate entrance examination in September
Study summary of postgraduate entrance examination in July
Yarn的基础介绍以及job的提交流程
【推荐系统 01】Rechub
CAS mechanism
Prototype and prototype chain
555 circuit details
软考信息处理技术员有哪些备考资料与方法?
2022年上半年5月网络工程师试题及答案
求方程ax^2+bx+c=0的根(C语言)
BigDecimal数值比较
深入理解Apache Hudi异步索引机制
ArrayList thread insecurity and Solutions
1324: [example 6.6] integer interval
PHP \ newline cannot be output
软考中级,软件设计师考试那些内容,考试大纲什么的?