当前位置:网站首页>两种特殊函数(箭头函数和方法)
两种特殊函数(箭头函数和方法)
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 中出现过!
边栏推荐
猜你喜欢

便携式钻孔测斜仪数据采集仪测量原理与测斜探头的连接及使用方法

以太网转RS485串口计数器WiFI模块 LED灯光控制器IBF165

占空比开关量输出高速脉冲计数器RTUModbus模块IBF63

MLX90640 红外热成像仪测温传感器模块开发笔记(八)
![[wechat applet development (VII)] subscription message](/img/aa/f0c68bc0c068ac208820df42cce3db.png)
[wechat applet development (VII)] subscription message

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

光学雨量计对比翻斗式雨量计的优势

虚拟机之NAT模式下设置静态IP

5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95

【直播预约】数据架构演进下的新挑战——上海站
随机推荐
远距离串口服务器( 适配器)UART/I2C/1-Wire/SPI PS304常见问题及注意事项
28.55亿元!青岛芯恩增资完成:兴橙集电成第一大股东,持股57.10%
Minimum heap improves the efficiency of each sort
An article about rsocket protocol
Framework customization series (I) -- systemui NavigationBar slide back to launcher on the navigation bar
Pyqt5 rapid development and practice 5.2 container: load more controls
Leetcode bracket validity problem
[channel attention mechanism] senet
Solve the problem that the right-click menu "edit with idle" of the 『 py 』 file is invalid or missing
Principle and application of low cost / small volume module rs485/232 to analog signal ibf33
A tour of gRPC:05 - gRPC server straming 服务端流
How to compress and decompress ramdisk.img
monkey压力测试
samba服务器如何配置
跟我学Rx编程——Concat
如何通过adb打开和关闭飞行模式
光学雨量计对比翻斗式雨量计的优势
管理区解耦架构见过吗?能帮客户搞定大难题的
突发!微星CEO江胜昌坠楼身亡
软件架构与设计(八)-----分布式架构