当前位置:网站首页>Closure, prototype and original link
Closure, prototype and original link
2022-07-28 22:29:00 【Crane sorrow_】
One 、 Closure
1. What is a closure
A closure is a function nested function ; Function is returned as a return value , Function is passed in as an argument .
2. The function of closures
- Create local scope
- The local variables inside the function can be accessed outside the function
- Package components and plug-ins
3. Disadvantages of closures
Free variables in closures are not automatically destroyed , Will reside in memory , Improper use can easily lead to memory leakage
4. Closure test site
Free variables in closures ( Variables used without definition in the scope of change ) It is determined when the function is defined , It is not determined when the function is executed
<script type="text/javascript">
var b = 100;
function fn (){
b++;
console.log(b);
}
function outer(callback){
var b = 500;
callback();
}
outer(fn);
// 01 Free variables in closures , It is determined when the function is defined , It's not the time to implement
// 02 Free traversal in closures does not destroy
</script>Two 、 Prototype and original link
1. What is the prototype and the original link
- Each structure function ( class ) There is a display prototype prototype
- Each instance ( object ) They all have an implicit prototype __proto__
- The implicit prototype of the object __proto__ Equal to its constructor ( class ) The display prototype of prototype
- When looking for the properties or methods of an object , Now find yourself , If you can't find it, follow the prototype of the prototype (__proto__.__proto__ Upward search )
- We call the chain relationship of prototype form as prototype chain
2. effect
- Realized js Inheritance
- You can add common methods and properties to all instances of classes
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1> How to create an object </h1>
<p> Create objects :1 Literal ,2 new One </p>
<script>
var obj = {name:"mumu",age:18};
console.log(obj);
function Animal(name,age){
this.name = name;
this.age = age;
}
Animal.prototype.say = function(){
alert(` Hello, my name is ${this.name}, This year, ${this.age} year `)
}
function Student(name,age,no){
Animal.call(this,name,age);
}
Student.prototype = Object.create(Animal.prototype);
Student.prototype.constructor = Student;
Student.prototype.study = function(){
alert(` Hello, I'm ${this.name}, I'm trying to learn the front end `);
}
var s1 = new Student(" Xiao Ming ",17,"001");
var s2 = new Student(" Little high ",22,"004");
</script>
</body>
</html>3、 ... and : Anonymous function self executing
1. Concept :
Anonymous function self executing , Similar to function nested function , Closures are also formed , You can create local variables ( Functions without names , It may only be called once , Or when passing parameters )
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<h1> Anonymous functions </h1>
<p> Anonymous function self executing , Similar to function nested function , Closures are also formed , You can create local variables </p>
<ul>
<li> list 1</li>
<li> list 2</li>
<li> list 3</li>
<li> list 4</li>
<li> list 5</li>
</ul>
<script type="text/javascript">
var lis = document.querySelectorAll("ul li");
for(var i=0;i<lis.length;i++){
(function(j){
lis[j].onclick = function(){
alert(j);
}
})(i)
}
</script>
</body>
</html>Four : Function class
- adopt new The function executed is called constructor ,( It is suggested to capitalize ) It can also be called a class
- adopt new Generating objects , An instance called a class ( Instance object )
- In the constructor this Point to the corresponding instance
5、 ... and : Inherit
1.js How to implement inheritance
- class Class can be used extends Keyword implementation inheritance
- Inheritance through prototype chain
2. Function class inheritance :
- Inheritance constructor
function Student(name,age,no){
Animal.call(this,name,age)
} - Inherit the prototype
Student.prototype = Object.creat(Animal.prototype) Fix constructor
Student.prototype.constructor =Stuent;// Customize Student Extension method
Student.prototype.study = function(){}
6、 ... and :this Pretend to be
1.add.call(obj,3,5)
perform add Function use obj To pretend add Of this,3 and 5 Is the parameter
2.add.apply(obj,[3,5])
perform add function , use obj Pretend to be this, The parameter transfer method is array
3.this Point to the object problem
- The browser directly this The point is window object
- In custom objects this Point to current object
- Class this Point to future objects
边栏推荐
- HCIP(15)
- [Ruiji takeout project] Day5 - Chapter 6 mobile verification code login
- Ordinary practice of JS DOM programming
- Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)
- Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
- mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
- hcip实验(12)
- Ruiji takeout project - development of business development function Day2
- [CS231N]Lecture_ 2:Image Classification pipelin
- Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
猜你喜欢

SQL注入 Less38(堆叠注入)

How to install WiFi correctly

Learning notes and summary of C language programming specification

HYDAC overflow valve db08a-01-c-n-500v

静态路由和缺省路由实验

Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints

Data visualization news, different forms of news reports

成立不到一年!MIT衍生量子计算公司完成900万美元融资

HCIP(13)
随机推荐
ATT&CK 威胁情报
What are the main functions and uses of LCR tester
Future trend of defi in bear market
Sword finger offer II 065. The shortest word code (medium dictionary tree string array)
20-09-27项目迁移到阿里折腾记录(网卡顺序导致服务无法通过haproxy连接到db)
HCIP(11)
2021 mathematical modeling group B exercise
What does GPRS network mean
Data visualization news, different forms of news reports
JMeter installs third-party plug-ins plugins Manager
乌官员:乌克兰一半农产品经多瑙河港口出口
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
SQL注入 Less42(POST型堆叠注入)
纪念一下第一次写的线段树了喽(对应洛谷3372)
HCIP(15)
Ruiji takeout project - development of business development function Day2
Learning notes and summary of C language programming specification
ngx+sql环境离线安装日志(rpm安装)
Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
Establishment of Ruiji takeout development environment