当前位置:网站首页>两种特殊函数(箭头函数和方法)
两种特殊函数(箭头函数和方法)
2022-07-28 14:51:00 【PBitW】
箭头函数总结

箭头函数没有prototype(原型),所以箭头函数本身没有this,要从上下文获取!
方法
在一个对象中绑定函数,该函数被称为这个对象的方法。
在JavaScript中,对象的定义是这样的:
var xiaoming = {
name: '小明',
birth: 1990
};
但是,如果我们给xiaoming绑定一个函数,就可以做更多的事情。比如,写个age()方法,返回xiaoming的年龄:
var xiaoming = {
name: '小明',
birth: 1990,
age: function () {
var y = new Date().getFullYear();
return y - this.birth;
}
};
xiaoming.age; // function xiaoming.age()
xiaoming.age();
绑定到对象上的函数称为方法,和普通函数也没啥区别,但是它在内部使用了一个this关键字,这个东东是什么?
在一个方法内部,this是一个特殊变量,它始终指向当前对象,也就是xiaoming这个变量。所以,this.birth可以拿到xiaoming的birth属性。
这个图在 01 邂逅vue 中出现过!
边栏推荐
- 12V脉冲转速测量转24V电平信号转换变送器
- Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module
- 一波骚操作解决Laya场景编辑器报错问题
- 数牍 X Rust,那些不得不说的事
- Open light input / relay output rs485/232 remote data acquisition IO module ibf70
- Framework customization series (10) -- systemui customization status bar statusbar and navigation bar tutorial
- A wave of operation to solve the error problem of Laya scene editor
- 开光量输入/继电器输出rs485/232远程数据采集IO模块IBF70
- 如何快速接入统一的认证鉴权体系
- PyQt5快速开发与实战 5.2 容器:装载更多的控件
猜你喜欢

Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!

Voice social system -- improve the audio system industry chain

How to effectively conduct the review meeting (Part 1)?

VM501开发套件开发版单振弦式传感器采集模块岩土工程监测

A tour of gRPC:05 - gRPC server straming 服务端流

js 优先级队列

12V脉冲转速测量转24V电平信号转换变送器

Shell programming specifications and variables

How to obtain and embed go binary execution package information

【微信小程序开发(七)】订阅消息
随机推荐
数牍 X Rust,那些不得不说的事
Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
玩死原型链
低成本/小体积模块RS485/232转模拟信号的原理以及应用IBF33
MLX90640 红外热成像仪测温传感器模块开发笔记(八)
samba服务器搭建指南
PyQt5快速开发与实战 5.1 表格与树
Rxdart is used instead of stateful in fluent
Framework customization series (10) -- systemui customization status bar statusbar and navigation bar tutorial
Virturalbox solves the problem of kernel driver
Perception of life
语音社交系统——完善有声系统产业链
Love programming
12V脉冲转速测量转24V电平信号转换变送器
使用systemd管理服务
如何快速接入统一的认证鉴权体系
Framework定制系列(一)-----SystemUI NavigationBar导航栏上滑返回Launcher
How as makes intelligent prompts regardless of case
Learn about the native application management platform of rainbow cloud
Framework定制系列(六)-----屏蔽FallbackHome手机启动中弹窗直接进入Launcher