当前位置:网站首页>JS knowledge points-01
JS knowledge points-01
2022-07-05 15:20:00 【Shepherd Wolf】
var let const difference
1. var Declaration upgrade ( Assignment does not raise ), The scope is within the function where the statement is located , Variables with the same name can be declared repeatedly ;
2. let There is no claim to promote , The scope is within the code block where the statement is located , Variables with the same name cannot be declared repeatedly , Modifiable variable value ;
3. const There is no claim to promote , The scope is within the code block where the statement is located , Variables with the same name cannot be declared repeatedly , The value of the variable cannot be modified ;
Variable Promotion :

Scope :


Determine whether it's an array or an object
Object.prototype.toString.call(a)

a instanceof xxx ( Return value : Boolean value ) You can determine whether it is an array , But you can't judge whether it's an array or an object :


typeof a ( Return value : character string ) , It is also impossible to judge whether it is an array or an object :

Array.isArray(a) You can determine whether it is an array :

Closure
Functions that can read internal variables of other functions . in other words , Function as a Inner function of b By the function a When an external variable is referenced , You create a closure .
characteristic :
1. Make it possible for external access to function internal variables ;
2. Local variables are resident in memory ;
3. You can avoid using global variables , Prevent contamination of global variables ;
4. Memory leaks can occur ( There's a block of memory that's been occupied for a long time , Without being released )
// As return value
function create () {
var a = 100
return function fn () {
console.log(a)
}
}
var a = 200
create()() // 100
// As a parameter
function create2 (fn) {
var a = 100
return fn()
}
var a = 200
function fn () {
console.log(a)
}
create2(fn) // 200 Closures and garbage collection mechanisms - Shepherd Wolf - Blog Garden Closure means having access Of local variables in the scope of other functions A function stay JS in , The scope of a variable belongs to the scope of a function , The scope will be cleaned up after the function is executed 、 Memory is also reclaimed , But because the closure is based on the sub function inside a function , because
https://www.cnblogs.com/edwardwzw/p/11754101.html
hasOwnProperty
hasOwnProperty() Used to detect whether the attribute is the object's own attribute , return true/false
let obj = {
name:'edward',
age:18,
eat:{
eatname:' meat ',
water:{
watername:' Nutrition Express '
}
}
}
console.log(obj.eat.hasOwnProperty('watername')) //false
console.log(obj.eat.water.hasOwnProperty('watername')) //true
Deep copy
1. JSON.parse(JSON.stringify(obj))
2.
function deepClone (source) {
const targetObj = source.constructor === Array ? [] : {}
for (let key in source) {
if (source.hasOwnProperty(key)) {
if (source[key] && typeof source[key] === 'object') { // Reference data type
targetObj[key] = deepClone(source[key])
} else { // Basic data type
targetObj[key] = source[key]
}
}
}
return targetObj
}apply call bind
var name = ' Xiao Wang ', age = 18
var obj = {
name: ' Xiao Ming ',
objAge: this.age,
myFun: function (a,b) {
console.log(this.name, this.age, a, b)
}
}
var obj1 = {
name: 'xiao li',
age: 6
}
var obj2 = {
name: 'edward',
age: 9
}
obj.myFun.apply(obj1, ['aaa', 'bbb']) // xiao li 6 aaa bbb
obj.myFun.call(obj2, 'aaa', 'bbb') // edward 9 aaa bbb
obj.myFun.bind(obj1, 'ccc', 'ddd')() // xiao li 6 ccc dddThe event loop (Event Loop)
js It's a single threaded scripting language , At the same time , Can only do the same thing , To coordinate events 、 User interaction 、 Script 、UI Rendering and network processing , Prevent the main thread from blocking ,Event Loop The plan came into being ...

js When the engine compiles the code :
When encountering synchronous code, it is directly put into the execution stack ,
Encountered an asynchronous code , It will not wait for the return result of asynchronous code , Instead, it hangs in the task queue
When all synchronization tasks in the execution stack are completed , The main thread is idle , You will find out whether there are tasks in the task queue . If there is , Then the main thread will take out the events in the first place , And put the callback corresponding to this event into the execution stack , then : When encountering synchronization code ... When asynchronous code is encountered ...
So again and again , This creates an infinite cycle . This is the process called “ The event loop (Event Loop)” Why .
In each round, micro tasks are performed first , Then execute the macro task .( If a round of micro tasks contains macro tasks , Put the macro task at the end of the current macro task list )
Asynchronous task It is divided into Macro task (macrotask) And Micro task (microtask)
Micro task :Promise async/await process.nextTick
Macro task :setTimeout setInterval Dom event Ajax request
Execution order : Micro task -> Dom Rendering -> Macro task

If we use alert The incident interrupted js, Will find :alert('promise then') Time page dom Did not start rendering , Turn off the alert after ,dom Render completed , then alert('setTimeout')
Draw a triangle
<!DOCTYPE html>
<html lang="en">
<head>
<title> triangle ( Arrow right )</title>
<style>
.t1 {
width: 0;
height: 0;
border-left: 20px solid red;
border-right: 20px solid transparent;
border-bottom: 20px solid transparent;
border-top: 20px solid transparent;
}
</style>
</head>
<body>
<div class="t1"></div>
</body>
</html>
边栏推荐
- I spring web upload
- MySQL----函数
- Fr exercise topic --- comprehensive question
- Change multiple file names with one click
- ICML 2022 | 探索语言模型的最佳架构和训练方法
- 1330:【例8.3】最少步数
- mapper.xml文件中的注释
- Thymeleaf uses background custom tool classes to process text
- Interview shock 62: what are the precautions for group by?
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
猜你喜欢

ionic cordova项目修改插件

Number protection AXB function! (essence)

No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle

超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜

Redis' transaction mechanism

I spring web upload

Select sort and bubble sort

机器学习笔记 - 灰狼优化

计算中间件 Apache Linkis参数解读

Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
随机推荐
Bugku cyberpunk
Bugku alert
Drive brushless DC motor based on Ti drv10970
Long list optimized virtual scrolling
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
Common MySQL interview questions (1) (written MySQL interview questions)
The elimination strategy of redis
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
Cartoon: programmers don't repair computers!
Fr exercise topic --- comprehensive question
Database learning - Database Security
Shanghai under layoffs
社区团购撤城“后遗症”
queryRunner. Query method
12 MySQL interview questions that you must chew through to enter Alibaba
MySQL之CRUD
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
Go learning ----- relevant knowledge of JWT
我这边同时采集多个oracle表,采集一会以后,会报oracle的oga内存超出,大家有没有遇到的?
Magic methods and usage in PHP (PHP interview theory questions)