当前位置:网站首页>Multiple types of functions
Multiple types of functions
2022-07-24 06:34:00 【PS notes】
The first one is :
function fun(n,m) {
return n+m;
}
let return1 = fun(10,20)
let return2 = fun(100,200)
console.log(return1);
console.log(return2);
The second kind , Function declaration
let return1 = fun(100,200)
console.log(return1);
function fun(n,m) {
return n+m;
}The third kind of : Anonymous functions , It's also called a callback function , Pass an anonymous function as a parameter to another function or method .
setInterval(function(){
console.log("hello");
},1000)
// It can be written like this
function fun(){
console.log("hello")
}
setInterval(fun,1000)A fourth : Function expression
const fun =function(n,m){ return n + m; } let result = fun(10,22); console.log(result);
The fifth :
Immediate execution function , Code encapsulation
(function(){
let a = 10;
let b = 20;
console.log(a + b);
})()6 kinds of :
let str= "hello";
function fun1(){
let str = "word";
let num = 10;
function fun2() {
console.log(str);
console.log(num);
}
fun2();
}
fun1 ();
console.log(str);
Seventh kinds : Closure function code encapsulation
const module= (
function(){
let a = 10;
let b = 20;
function add(){
return a+b;
}
return add;
}) ()Eighth : Arrow function :
Arrow function
const add= (x) => {
return x * x;
}
In the second simplification
const add = x => x * x ;
original
const fun = function (x) {
return x * x;
}
let result = fun(2);
console.log(result);
The arrow function is abbreviated as :
const fun = (x) => {
return x * x;
}
let result = fun(2);
console.log(result);
More concise :
const fun = x=> {
return x * x;
}
let result = fun(2);
console.log(result);
The simplest :
const fun = x=> x * x;
let result = fun(2);
console.log(result);The arrow function has another this Pointing is more important :
primary :
const cat = {
name :"miaomiao",
sangnn(){
let self = this;
window.setInterval(function(){
console.log(self.name);
},1000)
}
}
cat.sangnn();
After using the arrow :
const cat = {
name :"miaomiao",
sangnn(){
setInterval(() => {
console.log(this.name);
},1000)
}
}
cat.sangnn();
Arrow function and ordinary function this Point to different , A normal function refers to the object that calls the function .
Where the arrow function is defined, it points to who .
边栏推荐
- 日志收集分析平台
- PXE技术网络装机
- Interview questions for Test Manager / test team leader / Test Supervisor
- IP notes (8)
- history命令历史记录中加时间
- JSP tag 02
- IP notes (7)
- Log collection and analysis platform
- Mysql database - SQL summary (remember to pay attention to me! Come on in China!)
- [301] grotesque behavior - predictable irrationality
猜你喜欢

Data set and pre training model

XML parsing

A batch of interview questions and answers_ 20180403 latest arrangement

Remember to get the password of college student account once, from scratch

服务器硬件及RAID配置实战

手动安装Apache

Customize MVC 3.0

Use and principle of spark broadcast variable and accumulator

IP notes (11)

FTP服务与实验
随机推荐
Custom MVC 1.0
TensorFlow-GPU 安装 -- 056
Remote connection to Qunhui NAS at home [no public IP, free intranet penetration]
Solution of forgetting root password in mysql5.7 under Windows
UE4 reload system 1. basic principle of reload system
DHCP原理与配置
OpenSSL version upgrade
Top 10 vulnerability assessment and penetration testing tools
Luckyframeweb testing platform (a full latitude free open source testing platform that supports interface automation, Web UI automation, APP automation, and distributed testing)
Maximum value of jz47 gifts (dynamic planning ideas)
List of problems in the re disk guidance of the project
文件系统与日志分析
服务器硬件及RAID配置实战
CentOS操作系统安全加固
Leetcode sword finger offer jz73 flip word sequence
MySQL数据库—SQL汇总(记得关注我!中国加油!)
IP notes (11)
实验:磁盘配额操作
Batch implementation of key based authentication using sshpass
United Nations agricultural products data analysis