当前位置:网站首页>About prototype and prototype chain
About prototype and prototype chain
2022-06-11 09:38:00 【Snatch bamboo shoots 123】
Catalog
This blog reference :
About js Objects in the
js It is an object-oriented language , But in ES6 Before ,js Cannot create class , You can't create objects through classes , But through prototype
Constructor create object
because ES6 There is no concept of class in the previous syntax , So you can use constructors instead of classes to create objects , Grammar format
function Constructors (){
this. attribute ;
this. Method ;
}
// Create instantiated objects
var Object name =new Constructors ()
The difference between a constructor and a normal function : To invoke the former, you need to pass new keyword
Setting method of object properties
//1. Write properties through constructors
Constructors .prototype. Property name
//2. Add attributes when defining instance objects
Object name . Property name
Implementation inheritance
- Concept : Inherit , That is, reuse some original functions , Subclasses get the properties and methods under the parent class through inherited methods
- Realization way : By calling directly in the constructor
- adopt call() Method to implement inheritance
function Fo(){
this.name="Tom"
this.say=function(){
console.log(` Hello everyone , My name is ${
this.name}`)
}
}
function Ch(){
// Call constructor , change this Point to
Fo.call(this)
this.name="Jerry"
this.age=19
}
Ch.prototype.showage=function(){
console.log(` My age is ${
this.age}`)
}
let myobj1=new Fo("Tom")
let myobj2=new Ch("Jerry")
myobj1.say()
myobj2.say()
myobj2.showage()
The problem is
- The problem of constructor parameter passing is not solved
- Note the initial capitalization of the constructor
- Inheritance through prototype objects
After creating a constructor , Its default prototype object is Object, We can go through .prototype Property to modify its prototype object , So that the instance object created by this constructor has some properties and methods specified in
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> This is a html5 demo</title>
</head>
<body>
<script> function Animal(name,height){
this.name=name this.height=height this.bev=function(){
console.log(" I like playing ") } } function Cat(){
this.age=3 this.say=function(){
console.log(" Meow meow ") } } Cat.prototype=new Animal(" Little mi ",23) cat1=new Cat() console.log(cat1.name) cat1.bev() cat1.say() </script>
</body>
</html>
Chrome Browser run results 
Prototype
Concept : every last js object (null With the exception of ) Another object will be associated with it when it is created , This object is what we call prototype . Every object will be from prototype " Inherit " attribute
Create a new js Object time , Even if the new object is empty , When we output it , We found that it has an attribute prototype
Although we are not here obj Object , But you can also call some methods 
In fact, these methods are from prototype attribute Inherited from , in other words prototype attribute yes obj Prototype object
Prototype chain
Concept : A chain of interrelated prototypes
stay js in , Almost all objects are Object Instance of type , They all start from Object.prototype Inherit properties and methods .
Let's use an example to illustrate the situation
var date=new Date()
date.__proto__===Date.prototype// true
Date===Date.prototype //false
Date.prototype.__proto__===Object.prototype //true
Date.prototype===Object //false
typeof date //'object' Instance object
typeof Date //'function' Constructors
typeof Date.prototype //'object' Prototype object
typeof Object //'function' Constructors
typeof Object.prototype //'object' Prototype object
typeof null //'object'
actually ,object It's just one. js Built in constructor
Above we define an instance object , Let's take a look at such a situation
date.toString()
actually ,Date Method is not defined in constructor , It is Object The method defined in the constructor , Let's take a look through a flow chart , To pass through date The instance object calls the method ,js What has been done internally

From the above figure, we can see , When date There is no... In the instance object toString() When the method is used ,js The method will be found in the upstream prototype object along the prototype chain , stay Object Prototype object Object.prototype The method was found in , Completion of invocation . actually , The prototype chain is an example of inheritance
Properties,
prototype
Each function has one prototype attribute , This attribute points to an object , That is, the prototype of the instance created by calling this constructor
typeof Date.prototype //"object"
__proto__
every last js object ( except null ) All have a property , It's called __proto__, This property points to the prototype of the object .
Date.prototype.__proto__===Object.prototype //true
typeof Object.prototype //"object"
constructor attribute
Each prototype has a constructor Property points to the associated constructor .
Date.prototype.constructor===Date// true
边栏推荐
- 企业需要考虑的远程办公相关问题
- P1169 "chessboard making"
- When the enterprise makes a decision, which part should lead the ERP project?
- Do you know these advantages of ERP system?
- ESP8266_SmartConfig
- 考研數學 【數列極限證明題】題型方法總結
- ERP体系的这些优势,你知道吗?
- [image processing] spatial domain image enhancement
- 报错Output image is bigger(1228800B) than maximum frame size specified in properties(1048576B)
- Opencv oak-d-w wide angle camera test
猜你喜欢

MSF adds back door to normal program

Runtimeerror: blobreader error:the version of imported blob doesn't match graph_ transformer

Type-C docking station adaptive power supply patent protection case

Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community

A summary of the problem type and method for proving the limit of sequence in postgraduate entrance examination

Talk about reading the source code

Talk about how to customize data desensitization

Type-C蓝牙音箱单口可充可OTG方案

关于原型及原型链

OpenCV OAK相机对比及介绍
随机推荐
Openstack explanation (22) -- neutron plug-in configuration
[ROS] noedic moveit installation and UR5 model import
Slice of go language foundation
报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer
Flask (VI) - template
Error [error] input tesnor exceeded available data range [neuralnetwork (3)] [error] input tensor '0' (0)
2161. divide the array according to the given number
Opencv CEO teaches you to use oak (V): anti deception face recognition system based on oak-d and depthai
面试题 17.10. 主要元素
Set up redis highly available cluster environment
关于原型及原型链
js中关键字this的理解
POJ3250「Bad Hair Day」
2161. 根据给定数字划分数组
Why is it difficult to implement informatization in manufacturing industry?
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
[TiO websocket] III. The TiO websocket server can send messages to users anywhere
Analysis of high frequency interview questions in massive data processing
Detailed explanation of the difference between construction method and method
ESP8266_MQTT协议