当前位置:网站首页>那些易混淆的概念(三):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指向
边栏推荐
- Some properties of leetcode139 Yang Hui triangle
- 关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
- 对word2vec的一些浅层理解
- 求方程ax^2+bx+c=0的根(C语言)
- 嵌入式工程师如何提高工作效率
- Study summary of postgraduate entrance examination in November
- String formatting
- Fiddler break point
- I'd rather say simple problems a hundred times than do complex problems once
- IPv4 socket address structure
猜你喜欢
对存储过程进行加密和解密(SQL 2008/SQL 2012)
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
IIC Basics
Leetcode-560: subarray with sum K
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Leetcode exercise - 113 Path sum II
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
基于gis三维可视化技术的智慧城市建设
Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
随机推荐
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
Vs code specifies the extension installation location
String formatting
Appx code signing Guide
Programming features of ISP, IAP, ICP, JTAG and SWD
IO model review
@Transcation的配置,使用,原理注意事项:
IIC Basics
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
leetcode-304:二维区域和检索 - 矩阵不可变
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
Serial communication relay Modbus communication host computer debugging software tool project development case
【acwing】789. 数的范围(二分基础)
IPv4 socket address structure
Study summary of postgraduate entrance examination in July
IDA中常见快捷键
XML configuration file parsing and modeling
[dai6] mirror image of JZ27 binary tree
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Factorial implementation of large integer classes