当前位置:网站首页>Supplementary inheritance and strict mode
Supplementary inheritance and strict mode
2022-06-25 14:13:00 【Rabbit^-^】
Define how inheritance works
1. Prototype chain inheritance
function Father(){
this.color=["red","blue"]
}
function Son(){ }
Son.prototype=new Father()advantage : You can reuse the properties and methods under the parent class
shortcoming : Cannot pass a value to a variable under a parent class , Attributes are shared by children , One change, all change
2. Constructor inheritance
function Father(name){
this.color=["red","blue"]
this.name=name
}
function Son(name){
Father.call(this,name)
}advantage : The data under the parent class is not shared , Subclasses can be assigned with the help of the parent constructor
shortcoming : Code reuse cannot be realized , Each child assigns a copy of the parent class
3. Combination inheritance
function Father(name){
this.name=name// Borrowed
}
function Son(name){
Father.call(this,name)
}
Father.prototype.color=["red","blue"]// Shared
Son.prototype=new Father()advantage : You can find the property and method inheritance on the prototype , You can also pass parameters to the parent class
shortcoming : Every time you create a new object , Call the parent object constructor multiple times , Multiple... Will be generated in memory , Waste resources
4. Prototype pattern inheritance
var Father={color:["red","blue"]}
function person(obj){
function F(){}
F.prototype=obj
return new F()
}
var s1=person(Father)advantage : You can reuse the properties and methods under the parent class
shortcoming : Cannot pass value to parent variable , Properties are shared by child objects , Every change changes
5. Parasitic inheritance patterns
var Father={color:["red","blue"]}
function person(obj){
function F(){}
F.prototype=obj
var f= new F()
f.say=function(){
console.log(this.color)
}
return f
}advantage : You can reuse the properties and methods under the parent class , It's an enhanced version of the prototype pattern , You can add properties and methods by yourself
shortcoming : Cannot pass value to parent variable , Properties are shared by child objects , Every change changes
6. Parasitic combination inheritance
function Father(name){
this.name=name
}
Father.prototype.color=["red","blue"]
function Son(name){
Father.call(this,name)
}
function fun(son,father){
son.prototype=Object.create(Father.prototype)
son.prototype.constructor=son
}
fun(Son,Father)
var s1=new Son("asd")advantage : You can use the parent constructor , Prototype data can be shared , You can create different instance objects , Can achieve deep copy , Solve the waste of resources
Strict mode
To eliminate js Unreasonable and lax code in grammar , Make code run safer , Improve compilation efficiency
Definition :
"use strict"
Limited content
- The variable must use var Statement
- The top level of the function is no longer pointing to window, It's pointing to undefined
- Cannot define and eval A variable or function with the same name
- No use delete Keyword deletion
- No use argument, and calee and caller Method
- No use width sentence
- Duplicate function names are prohibited
- Object property name duplication is prohibited
- The use of octal numbers is prohibited
- It is not allowed to declare a function in a non functional code block
for example :
"use strict"
a=12
console.log(a)// no need var Declaration error
// Out-of-service with sentence
with(location){
console.log(href)
console.log(location.pathname)
console.log(location.port)// Port number
}边栏推荐
- 如何在 2022 年为 Web 应用程序选择技术堆栈
- 请问通达信股票开户是安全的吗?
- Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched
- 通达信股票账户开户安全吗
- Gorm-- search you don't know
- Application of tactile intelligent sharing-rk3568 in financial self-service terminal
- JVM 用工具分析OOM经典案例
- 一次性总结:64个数据分析常用术语!
- Solving error: creating window glfw error: glew initialization error: missing GL version
- 阻止深度神经网络过拟合(Mysteries of Neural Networks Part II)
猜你喜欢

Graph contractual learning with augmentations

Logistic Regression VS Linear Regression

Application of tactile intelligent sharing-rk3568 in financial self-service terminal

一次性讲清楚 Handler 可能导致的内存泄漏和解决办法 | 开发者说·DTalk

sigmoid函数sigmoid求导

解决报错:Creating window glfw ERROR: GLEW initalization error: Missing GL version

删库跑路、“投毒”、改协议,开源有哪几大红线千万不能踩?

深入理解深度神经网络背后的数学(Mysteries of Neural Networks Part I)

shell 内置命令

'NVIDIA SMI' is not an internal or external command, nor is it a runnable program or batch file
随机推荐
VGA display of de2-115 FPGA development board
Golang project dependency management tool go vendor, go Mod
多臺雲服務器的 Kubernetes 集群搭建
Two methods to rollback the code in pycharm to the specified version (with screenshot)
[world history] Episode II: Dawn of civilization
SSH secret free function for # scripting
Discriminative v.s.Generative
Deploy eve-ng with KVM virtualization
What are the red lines of open source that should not be trodden on?
【世界历史】第二集——文明的曙光
Openstack learning notes (I)
Installation and removal of MySQL under Windows
程序員為什麼要軟一點?
专家建议|8大措施加速你的创新职业规划和成长
完整详细的汇编实验报告
shell 数组
Table de hachage, conflit de hachage
权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?
Asp. Net webform exporting excel using npoi
深入理解深度神经网络背后的数学(Mysteries of Neural Networks Part I)