当前位置:网站首页>For loops and functions
For loops and functions
2022-07-28 22:29:00 【Crane sorrow_】
for loop :
Concept : Do repetitive things , Usually associated with numbers
The basic structure :for(;;){}( Two semicolons cannot be omitted )
One 、for Pay attention
- Whether the conditions are met :( If not satisfied , The code is not executed at one time )
- Is there an end condition ( without , It's a dead cycle ( Wireless loop ))
- browser 15 Seconds have not reacted , Will automatically crash
Two 、break
- break stay for Just... In the loop break The code is executed for Cycle and termination
3、 ... and 、continue
- continue When this cycle continue The code after the keyword does not execute the next cycle
var i = 1;
for(i=1;i<=500;i++){
console.log(i);
}var ps = document.querySelectorAll("p");
for(var i=0;i<ps.length;i++){
ps[i].onclick = function(){
alert(this.innerHTML);
this.classList.toggle("active");// Switch active Class name
}
}var ps = document.querySelectorAll("p");
for(var i=0;i<ps.length;i++){
console.log(ps[i].innerHTML);
}function :
One 、 Defined function
- function name (){ Code }
- A function is a block that can be executed repeatedly ( call ) Code block for
function fun(){
console.log(" Raising sheep ");
console.log(" Feed the horse ");
console.log(" Kindling wood ");
console.log(" eagle ");
}Two 、 Call function
- 1. stay js Call directly :fun()
- 2. Sure html Event properties :<h1 οnclick="fun()">
- 3.dom1 Level event registration :btn.onclick = fun
- 4.dom2 Level event registration :btn.addEventListener("click",fun)
- 5. Calling function in function
function fun(){
console.log(" Raising sheep ");
console.log(" Feed the horse ");
console.log(" Kindling wood ");
console.log(" eagle ");
}
//js Call directly
fun();
// stay dom1 Event call
var btn = document.querySelector("button");
btn.onclick = fun;3、 ... and 、 The parameters of the function
- parameter list : Function has a very special variable arguments, An array like variable storage function passes in a parameter list
- Formal parameters can be used at will , Just be consistent
function say(name){
alert(" I like it "+name)
}
say(" Apple ");
say(" Xiao Tong ");Four 、 Multiple parameters
function add(a,b){
var sum = 0;
for(var i=a;i<=b;i++){
sum+=i;
}
alert(sum);
}
add(2,5);5、 ... and 、 Function default parameters
function send(name="rose"){
alert(` dear ${name}, I miss you `);
}
send(" Xiao Tong ");
send();
send(" floret ");
send();6、 ... and 、 The return value of the function
- Function returns... By default undefined
- use return Keyword can define the function return value
- return Later code Function does not execute
function add(a,b){
return a+b;
alert(a+b);
}
var re = add(3,4);
console.log(re);7、 ... and 、 Anonymous functions
- Functions without names
- Register at event :el.onclick = function(){}
- As a callback function :list.forEach(function(item){})
- Anonymous function self executing :(function(){...})
- Assign a value to a variable and declare it before executing :var abc =function(){...}
//1. Bound with events
var h1 = document.querySelector("h1");
h1.onclick = function(){
alert(" Hello ");
}
//2. As a callback function
var ps = document.querySelectorAll("p");
ps.forEach(function(item){
console.log(item.innerText)
});
//3. Self execution
(function(){alert(" I like you ")})()
!function(){alert(" I don't like you ")}()
8、 ... and 、 Scope
Global scope :
- stay js You can visit anywhere
- stay script use var Declared variables
- window The global properties of
- Variables that are not declared with keywords anywhere are also global variables
Function scope :
- In the current function , Subfunctions can access
- Variables declared in the current function
- Function can access its parent element scope / Global scope variables cannot access function internal variables outside the function
- if,switch,else,for No scope
var a =15;
function say(){
var b =5;
function son(){
alert(a+b);
}
son();
}
say();
alert(b);边栏推荐
- Overall introduction of Ruiji takeout project
- 纪念一下第一次写的线段树了喽(对应洛谷3372)
- TensorFlow Serving 高性能的机器学习模型服务系统
- SQL injection less42 (post stack injection)
- Alibaba cloud CDN practice
- From Web3 to web2.5, is it backward or another way?
- 静态成员static详解
- Is mov format a still image file format
- XXX port is already in use
- flask之蓝图 补充openpyxl
猜你喜欢

Jmeter 安装第三方插件 Plugins Manager

mysql create语句能不能用来建立表结构并追加新的记录

HCIP(8)

Changes in the history of oscilloscope development

Add DNS server to LAN for domain name resolution

Ngrok intranet penetration

Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)

SQL injection less38 (Stack Injection)

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

Alibaba cloud CDN practice
随机推荐
软考网络工程师
gprs网络指的是什么
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
log4j漏洞 elk平台 处理方法 (logstah5.5.1)
Future trend of defi in bear market
[Ruiji takeout] day05 package management business development
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation
ssh 免密码登录
76. Minimum coverage substring (hard sliding window hash table string)
tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
array_ diff_ The method of not comparing array values when Assoc element is an array
32. Longest valid bracket (difficult stack string)
internet的基本服务中文件传输命令是哪个
40. Combined sum II
Add DNS server to LAN for domain name resolution
Openresty request authentication
Ruiji takeout - background login function development
NPM switch Taobao source (NPM source)
flask之蓝图 补充openpyxl
Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)