当前位置:网站首页>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);边栏推荐
- Soft exam network engineer
- hcip实验(14)
- Form validation and cascading drop-down lists (multiple implementations)
- 76. Minimum coverage substring (hard sliding window hash table string)
- vuejs中如何实现动态路由切换及路由的缓存
- Learning notes and summary of C language programming specification
- MySQL installation and configuration (super detailed, simple and practical)
- Ukrainian officials: half of Ukrainian agricultural products are exported through the Danube port
- Hcip experiment (14)
- Netease Yunxin 2022q2 product supply station, come and get your product supply plan!
猜你喜欢

HCIP(12)

DHCP and PPPoE protocols and packet capture analysis

hcip实验(14)

普源示波器实际的使用效果怎么样

How about the actual use effect of common source oscilloscope

Ngrok intranet penetration

79. Word search (medium string array matrix backtracking)

HCIP(11)

Ultra detailed visual studio 2019 running littlevgl (lvgl) simulator

Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
随机推荐
Sword finger offer II 063. replacement word (medium prefix tree string)
JS convert numbers to letters
Ruiji takeout project - development of business development function Day2
SQL injection less42 (post stack injection)
Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom
XXX port is already in use
96. Different binary search trees (medium binary search tree dynamic planning)
2021 mathematical modeling group B exercise
AWK空行过滤
Intranet penetration learning (III) horizontal movement of domain - planning tasks
[leetcode] maximum depth of binary tree
How about the actual use effect of common source oscilloscope
[Ruiji takeout] day05 package management business development
HCIP(10)
Future trend of defi in bear market
openresty 请求鉴权
HCIP(8)
Part 8: creating camera classes
Static details of static members
How to realize dynamic route switching and route caching in vuejs