当前位置:网站首页>ES6高级-利用构造函数继承父类属性
ES6高级-利用构造函数继承父类属性
2022-07-26 02:24:00 【setTimeout()】
一、call函数的作用
(1)调用函数
<body>
<script>
function sy() {
console.log('sy');
}
sy.call();
</script>
</body>输出结果:sy
(2)更改函数this指向
<body>
<script>
function sy(x, y) {
console.log('sy');
console.log(this);
console.log(x + y);
}
var obj = {
name: 'oliver'
}
sy.call(obj, 1, 2);
</script>
</body>输出结果:

二、利用构造函数继承父类属性
核心原理:通过call()方法把父类的this指向子类的this,这样就可以实现子类继承父类属性
<body>
<script>
function Father(uname, age) {
this.uname = uname;
this.age = age;
}
function Son(uname, age) {
Father.call(this, uname, age);
}
var son = new Son('son', 18);
console.log(son);
</script>
</body>输出结果:
![]()
边栏推荐
- 图解B+树的插入过程
- [2019] [paper notes] tunable THz broadband absorption based on metamaterials——
- The third question of leetcode 302 weekly Games -- query the number with the smallest k after cutting the number
- These practical security browser plug-ins improve your efficiency
- 18.删除链表的倒数第n个节点
- Turn on the LED
- Mandatory interview questions: 1. shallow copy and deep copy_ Deep copy
- What can EAM system help enterprises do?
- [early knowledge of activities] list of recent activities of livevideostack
- ERROR: could not extract tar starting at offset 000000000000020980+9231072+2
猜你喜欢

I.MX6UL核心模块使用连载-eMMC读写测试 (四)

I came to the library applet one click sign in and one click grab location tool

Quick start of adding, deleting, modifying and checking business

I.MX6UL核心模块使用连载-以太网测试 (七)

Bitmap这个“内存刺客”你要小心~

Handling process of the problem that the virtual machine's intranet communication Ping fails

I.MX6UL核心模块使用连载-nand flash读写测试 (三)

How to choose cloud note tool? What can I do with cloud notes?

C unit test

Are you still using ==0 null equal to judge null values? How much do you know about isempty and isblank
随机推荐
18. Delete the penultimate node of the linked list
[pyqt5 packaged as exe]
力扣148:排序链表
[red team] att & CK - using bits services to achieve persistence
C language -- program environment and preprocessing
What is JSX?
项目管理:精益管理法
1. Mx6ul core module serial USB interface test (VI)
scipy.sparse.vstack
numpy.sort
Overview of pytoch's API
1. Mx6ul core module use serialization - view system information (II)
[纯理论] YOLO v4: Optimal Speed and Accuracy of Object Detection
数仓:浅谈银行业的数仓构建实践
Prometheus + process exporter + grafana monitor the resource usage of the process
I.MX6UL核心模块使用连载-eMMC读写测试 (四)
Be careful about bitmap, the "memory Assassin"~
prometheus+blackbox-exporter+grafana 监控服务器端口及url地址
[C language brush leetcode] 1604. Warn people who use the same employee card more than or equal to three times within an hour (m)
i. Mx6ull snvs power domain GPIO status hold verification