当前位置:网站首页>Js理解之路:写一个比较完美的组合继承(ES5)
Js理解之路:写一个比较完美的组合继承(ES5)
2022-07-26 00:01:00 【The..Fuir】
Perfect?
function Animal(...rest){ Object.assign(this,{...rest}); } Animal.prototype.say=function say(){ console.log('wang'); } function Dog(...rest){ Animal.call(this,...rest) Object.setPrototypeOf(Dog.prototype,Animal.prototype); } Dog.prototype.sayOnce=function sayOnce(){ console.log('say once'); } const dog=new Dog('狗狗','猫猫'); dog.say(); dog.sayOnce();
是不是又很多疑惑点???
Object.assign是什么? Object.setPrototypeOf是什么?
Object.setPrototypeOf()方法
千次阅读2020-10-02 11:59:00
1.Object.setPrototypeOf(),为现有对象设置原型,返回一个新对象
接收两个参数:第一个是现有对象,第二是原型对象。
2、new 命令通过构造函数新建对象实例的过程,其本质是将实例的原型,指向了构造函数的prototype属性,然后在实例上执行构造函数
Object.assign是什么?
基本语法
Object.assign(target, ...sources)
基本概念
Object.assign方法用来将源对象(source)的所有可枚举属性,复制到目标对象(target)。它至少需要两个对象作为参数,第一个参数是目标对象,后面的参数都是源对象。只要有一个参数不是对象,就会抛出TypeError错误。
基本用途
1、合并多个对象
var target = { a: 1, b: 1 }; var source1 = { b: 2, c: 2 }; var source2 = { c: 3 }; Object.assign(target, source1, source2); // {a:1, b:2, c:3}
边栏推荐
- 1223. Dice simulation range DP
- Exercise (2) create a set to store the elements "1", "$", "2", "$", "3", "$", "4"“
- Prometheus 运维工具 Promtool (二) Query 功能
- 多御安全浏览器手机版将增加新功能,使用户浏览更个性化
- Shardingsphere data slicing
- LeetCode_55_跳跃游戏
- Key features and application trends of next generation terminal security management
- 二叉树——101. 对称二叉树
- Exercise (3) create a list set (both ArrayList and LinkedList)
- Binary tree - 654. Maximum binary tree
猜你喜欢

Lua script Wireshark plug-in to resolve third-party private protocols

LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果

Binary tree -- 104. Maximum depth of binary tree

浅识 OWASP

调用钉钉api报错:机器人发送签名过期;solution:签名生成时间和发送时间请保持在 timestampms 以内

Compile live555 with vs2019 in win10

SQLZOO——Nobel Quiz

Part 74: overview of machine learning optimization methods and superparameter settings
![[learning notes] unreal 4 engine introduction (IV)](/img/30/4defa3cbd785d43adb405c71d16406.png)
[learning notes] unreal 4 engine introduction (IV)

Yolov4 tiny network structure
随机推荐
Binary tree -- 222. Number of nodes of a complete binary tree
SIGIR '22 recommendation system paper graph network
二叉树——111. 二叉树的最小深度
LeetCode_55_跳跃游戏
LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果
行为型模式之迭代器模式
Stm32 systeminit trap during simulation debugging
Binary tree -- 104. Maximum depth of binary tree
Weight file and pre training file of yolov3
Seventy second: pedestrian detection
Leetcode169 detailed explanation of most elements
Program environment and pretreatment
STM32 lighting procedure
【英雄哥七月集训】第 24天: 线性树
模块二作业
Binary tree - 404. Sum of left leaves
Leetcode shahutong series -- 63. Different paths II
二叉树——110. 平衡二叉树
栈与队列——150. 逆波兰表达式求值
Sort fake contacts








