当前位置:网站首页>Do you really understand the things about "prototype"? [part I]
Do you really understand the things about "prototype"? [part I]
2022-07-05 10:45:00 【How's Kakashi doing】
List of articles :
1.2 Constructors new Implementation process of
1.3 Instance members and static members
Two : Prototype object prototype
2.1 Why are there prototype objects
2.2 The use of prototype objects
3、 ... and : Object prototype __proto__
3.1 What is an object prototype ?
3.2 About object prototypes __proto__ Points for attention
Four : Constructors constructor
4.1 Why? constructor It's also called a constructor
4.2 Manual return constructor The situation of
What is a prototype ? The prototype is JS Concepts we haven't mentioned in basic learning , Prototype is a general term , Mainly consists of Prototype object (prototype) , Object prototype (__proto__), Prototype chain wait , According to statistics, these concepts are also often asked in interviews , This article will take you to understand and master the relevant knowledge of prototype , Let everyone no longer be confused .
One : Constructors
We have learned many object-oriented languages , for example java c++ wait , however JavaScript The exception is , stay ES6 Before , There is no concept of a class , How do we create objects before ? Originally in ES6 Before , We are using Constructors To create an instantiated object , A constructor is a special function , Contains the common features of the object , To cooperate new It makes sense to use them together .
Constructor considerations :
- The first letter of the constructor name should be capitalized
- Constructors should match new Use it together
1.1 Constructor usage
<script>
function Animal(name,age){ // The constructor name is capitalized
this.name=name;
this.age=age;
this.eat=function(){
console.log(' I'm eating ');
}
}
var dog=new Animal(' Wangcai ',3) // To cooperate new Use together to create objects
console.log(dog.name);
console.log(dog.age);
dog.eat()
</script>
1.2 Constructors new Implementation process of
new There are the following execution processes :
- new An empty object will be created when
- In constructor this Point to this empty object
- Execute the code in the constructor to assign a value to the empty object , Add attribute method
- Return this object
1.3 Instance members and static members
Instance members :
- Instance members are for internal use this Added members
- Instance members can only be accessed through instantiated object calls , Cannot access through constructor name
<script> function Animal(name,age){ this.name=name; this.age=age; } var dog=new Animal(' Wangcai ',3) console.log(dog.name); console.log(Animal.name); </script>
Static members :
- Static members are members created by the constructor itself
- Static members can only be accessed through constructor names , Cannot access through instantiated objects
<script> function Animal(name,age){ this.name=name; this.age=age; } var dog=new Animal(' Wangcai ',3) Animal.color=' black ' console.log(Animal.color); console.log(dog.color); </script>
Two : Prototype object prototype
2.1 Why are there prototype objects
Before starting to prototype what the object is , Let's start with a case , It's the same one just now Animal class , We created multiple instantiated objects , A comparison of two methods of outputting instantiated objects , We found the output false, That is, the addresses of the two complex data types are different , What's the reason ?
<script> function Animal(name,age){ this.name=name; this.age=age; this.eat=function(){ console.log(' I'm eating '); } } var dog=new Animal(' Wangcai ',3) var cat=new Animal(' Mimi ',3) var pig=new Animal(' Hem ',3) var fish=new Animal(' Murmur ',3) var sheep=new Animal(' BAABAA ',3) console.log(dog.eat==cat.eat); </script>
In the process of creating instantiated objects ,new The process of creating a new object first , But complex data types will open up a space for storage ( object , Method ), This is This causes the same method in the constructor to be opened up countless blocks of memory , Caused extreme waste of memory
2.2 The use of prototype objects
Constructor prototype prototype Is an attribute in the constructor , Its attribute is a pointer , Point to an object , This object stores public methods , Methods that exist in this object , When you create an instantiated object through the constructor, you can make public use of this method , There is no need to open up multiple duplicate memory spaces for multiple complex data types . It is to solve the above problem of memory waste , It can also be directly referred to as the prototype object .
The above case solution :
Solution we use prototype objects to store public methods , And let the instantiated object call the method , And compare whether the addresses of the two are the same
<script>
function Animal(name,age){
this.name=name;
this.age=age;
}
Animal.prototype.eat=function(){
console.log(' I'm eating ');
}
var dog=new Animal(' Wangcai ',3)
var cat=new Animal(' Mimi ',3)
dog.eat()
cat.eat()
console.log(dog.eat==cat.eat);
</script>
We found that not only did we successfully call this method , And the addresses of the two methods are the same , This proves , Its common complex data types only open up a memory space , It reduces the waste of resources written in the constructor of public methods .
3、 ... and : Object prototype __proto__
3.1 What is an object prototype ?
Object prototype __proto__ The function of is to let you figure out a problem : Why is it given to constructors prototype Attribute adding method , Instantiated objects can use ? This is because every object has a __proto__ attribute ( Note that there are two underscores before and after ), This attribute is also a pointer , Refers to the prototype object of its corresponding constructor prototype, This explains why the instantiated object can call methods in the prototype object .
Or it can be understood as :
- Prototype object prototype Equivalent to Object prototype __proto__
3.2 About object prototypes __proto__ Points for attention
We should pay attention to the object prototype __protp__ The function of is only to provide a direction for finding the content in the prototype object , We don't need to use it , Just remember that it points to the prototype object of the corresponding constructor prototype that will do
Search principle of method :
- First, find out whether there is a target method on the constructor that instantiates itself , If there is one, call
- If there is no , Because the object itself has properties __protp__, It points to the prototype object of the constructor prototype, Will find out whether the prototype object has this method
Four : Constructors constructor
4.1 Why? constructor It's also called a constructor
Object prototype __proto__ Prototype object of body and constructor prototype I have one on me constructor attribute , The name constructor It's called a constructor , Because this attribute points to the corresponding constructor itself , It is mainly used to record which constructor the instantiated object refers to
Print both constructor attribute :
<script> function Animal(name,age){ this.name=name; this.age=age; } Animal.prototype.eat=function(){ console.log(' I'm eating '); } var dog=new Animal(' Wangcai ',4) console.log(dog.__proto__.constructor); console.log(Animal.prototype.constructor); </script>
We found that the printed result is indeed the constructor itself
4.2 Manual return constructor The situation of
More often, we need to return manually constructor Which constructor is pointed to , For example, when multiple public methods are stored as objects in the prototype object of the constructor , The following will happen :
<script> function Animal(name,age){ this.name=name; this.age=age; } Animal.prototype={ eat:function(){ console.log(' I'm eating '); }, run:function(){ console.log(' I'm running '); } } var dog=new Animal('wangchai',3) console.log(Animal.prototype.constructor); console.log(dog.__proto__.constructor); </script>
We found that it can't find the corresponding constructor , This is caused by the way we add methods to its prototype objects , The money we take is . Way to add , It is added on the original basis , It will not overwrite the original content inside . And we use = Methods of are added as objects , It is actually a process of assignment , The original content is also overwritten , This leads to prototype Internal original constructor Methods are overwritten
At this time, we need to return manually constructor To find out which constructor is returned
<script> function Animal(name,age){ this.name=name; this.age=age; } Animal.prototype={ constructor:Animal, eat:function(){ console.log(' I'm eating '); }, run:function(){ console.log(' I'm running '); } } var dog=new Animal('wangchai',3) console.log(Animal.prototype.constructor); console.log(dog.__proto__.constructor); </script>
So we can successfully get it constructor Which constructor is pointed to
The format for :
- constructor : Constructor name
That's all for this article , The next one is more wonderful !!
边栏推荐
- Taro进阶
- Learning Note 6 - satellite positioning technology (Part 1)
- AD20 制作 Logo
- 第五届 Polkadot Hackathon 创业大赛全程回顾,获胜项目揭秘!
- Web3 Foundation grant program empowers developers to review four successful projects
- DOM//
- 图片懒加载的方案
- beego跨域问题解决方案-亲试成功
- How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
- Secteur non technique, comment participer à devops?
猜你喜欢
DGL中的消息传递相关内容的讲解
Secteur non technique, comment participer à devops?
32:第三章:开发通行证服务:15:浏览器存储介质,简介;(cookie,Session Storage,Local Storage)
手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
In the year of "mutual entanglement" of mobile phone manufacturers, the "machine sea tactics" failed, and the "slow pace" playing method rose
Pseudo class elements -- before and after
AD20 制作 Logo
2022鹏城杯web
Learning note 4 -- Key Technologies of high-precision map (Part 2)
[vite] 1371 - develop vite plug-ins by hand
随机推荐
Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
beego跨域问题解决方案-亲试成功
SQL Server 监控统计阻塞脚本信息
2022年化工自动化控制仪表考试试题及在线模拟考试
风控模型启用前的最后一道工序,80%的童鞋在这都踩坑
Go language-1-development environment configuration
【SWT组件】内容滚动组件 ScrolledComposite
《通信软件开发与应用》课程结业报告
Go project practice - Gorm format time field
Pseudo class elements -- before and after
Share Net lightweight ORM
Activity enter exit animation
Learning II of workmanager
【Vite】1371- 手把手开发 Vite 插件
九度 1480:最大上升子序列和(动态规划思想求最值)
In wechat applet, after jumping from one page to another, I found that the page scrolled synchronously after returning
基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平
Implement the rising edge in C #, and simulate the PLC environment to verify the difference between if statement using the rising edge and not using the rising edge
C语言活期储蓄账户管理系统
各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f