当前位置:网站首页>Common APIs (Methods) for scope -number and string
Common APIs (Methods) for scope -number and string
2022-06-27 22:31:00 【Program Altman】
What is scope ?
Scope : A range in which variables work Because every variable Have their own corresponding living environment
Global scope : By script Packages can be called global scopes
Function scope
Each function Will generate an independent function scope
Variables declared within the scope of a function Can only be used within the scope of the current function Cannot access in other scopes
Function scope within function scope It belongs to the inclusion relationship The inner scope can access the outer scope But the scope inside cannot be accessed from the outside
Empathy The function scope cannot be accessed globally Function scope can access global
Block scope recognize { } Is the domain
There is no intersection between scopes Can't visit each other
// Global scope
let a = 1
function zhuque(){
console.log(a);
}
zhuque()
// Function scope
function zhuque(){
let a = 123
console.log(a);
}
zhuque()
// Block scope
if(true){
}
Variables declared within the scope of a function , Can only be used within the scope of the current function , Cannot access... In another scope
function zhuque(){
let a = 123 // The statement in Function scope
// console.log(b); // Dad cares about his son , It's impossible
function zhu1(){
console.log(a); //123
// let b = 456
}
zhu1()
console.log(a); //123
}
zhuque()
// There is no intersection between scopes Can't visit each other
There is no intersection between scopes Can't visit each other
function zhuque(){
let a = 0
function zhu1(){
let b = 456
}
zhu1()
}
zhuque();
function fn(){
console.log(a);
}
fn()
// Variables cannot be returned
function zhuque(){
let a = 0
return a
}
let num = zhuque();
console.log(num);
function fn(){
console.log(a);
}
fn()
When we are in a certain scope , When you need to use variables , First, find the variable from its scope
without , Just query the outer scope If there is no outer scope , Keep looking for the outer layer , Until the global scope is found
If there are no global variables , You're going to report a mistake
let a = 1
function fn(){
let a = 2
console.log(a); //2
{
console.log(a); //2
}
}
fn()
console.log(a); //1
let fn2 = function(){
{
console.log(a); //1
}
}
fn2()
No matter what scope As long as there is no statement Directly assigned Will become window Properties of
b = 1
window.b = 1
Number Methods (api)
parseInt(): Keep the whole number
parseFloat(): Keep floating point numbers
Number.isNaN(): Judgment is not NaN ( Returns a Boolean value )
Number.isFinte(): Judge whether it is a finite number ( Returns a Boolean value )
Number.isInteger(): Judge whether it's an integer ,( Returns a Boolean value )
toFixed(): Keep a few decimal places
1. Return integer ,(Number It can be omitted )
let num = 3.12344
console.log(Number.parseInt(num));//3
console.log(parseInt(num));
1. String to number , Cut off the non numeric part
let num = "123px"
console.log(parseInt(num)); //123
console.log(parseInt("a23b")); //NaN
2. Return floating point number ,(Number It can be omitted )
let num = 3.1548
let num1 = "123.12px"
console.log(Number.parseFloat(num));// 3.1548
console.log(parseFloat(num1));
3. Judge whether a value is NaN, Returns a Boolean value
let num = NaN
if(Number.isNaN(num)){
console.log(1);
}
if(Number.isNaN("abc" - 1)){
console.log(1);
}
4. Judge if it's a finite number ,( It can be omitted Number)
let num = 123
console.log(Number.isFinite(num)); //true
5. Judge whether it's an integer , Returns a Boolean value ( Don't omit Number)
let num = 3.1548
let num2 = 123
console.log(Number.isInteger(num));//false
console.log(Number.isInteger(num2));//true
6.toFixed() Keep a few decimal places
let num = 3.12344
console.log(num.toFixed(2)); //3.12
console.log(+Number(3.1525).toFixed(3));
Common use of strings API
1.charAt(): Get a string item Parameter serial number , Take the character corresponding to the sequence number of the string
let str = "zhuque520"
console.log(str.charAt(2));//u
console.log(str.charAt(str.length-1));//0
2.charCodeAt(): Take the sequence number corresponding to the string unicode code
let str = "zhuque520"
console.log(str.charCodeAt(str.length-1));//48
console.log(str.charCodeAt(0));//122 Return string unicode Encoding value
3.String.formCharcode(), adopt uniclde Sort value , Returns the corresponding character
console.log(String.fromCharCode(122)); //z
4.substring(strNum,endNum) Intercepting string from startNum Intercept to endNum ( Left closed right away )
Returns the new string successfully intercepted
let str = "zhuque520"
console.log(str.substring(0,3));//zhu
5.slice and substring Use the same ( It's all closed on the left and open on the right )
let str = "zhuque520"
console.log(str.slice(0,3));//zhu
6.*substr: Intercepting string , Cut by length
let str = "zhuque520"
console.log(str.slice(0,3));//zhu
7.*trim() Clear the left and right spaces , No matter in the middle
let str = " User groups "
console.log(str.trim());
8.replace(str,req) Replace string , Only the first retrieved string item will be replaced
let str = "zhuque520"
console.log(str.replace("zhuque"," Brother Yao "));
9.split() Cut the string into an array , Accept parameters , Is the cutting point ( According to what to cut )
let str = "12345"
// let arr = str.split() //['12345']
// let arr = str.split(" ") // According to the space ['12345']
// let arr = str.split("")// By empty character ['1', '2', '3', '4', '5']
let arr = str.split("3") //['12', '45']
console.log(arr);
边栏推荐
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
- Where can I set the slides on the front page of CMS applet?
- 北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
- Test automatique de Test logiciel - test d'interface de l'introduction à la maîtrise, apprendre un peu chaque jour
- Dynamic refresh mapper
- A method of go accessing gbase 8A database
- 【mysql实战】查询语句实战演示
- Go language slice vs array panic: runtime error: index out of range problem solving
- 如何做好功能测试?你确定不想知道吗?
猜你喜欢

7 jours d'apprentissage de la programmation simultanée go 7 jours de programmation simultanée go Language Atomic Atomic Atomic actual Operation contains ABA Problems

Windwos 8.1系统安装vmware tool插件报错的解决方法

管理系統-ITclub(下)

爬虫笔记(1)- urllib

Go from introduction to actual combat - task cancellation (note)

PCIe knowledge point -008: structure of PCIe switch

Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem

Système de gestion - itclub (II)

Learn to go concurrent programming in 7 days go language sync Application and implementation of cond

Management system itclub (medium)
随机推荐
解决本地连接不上虚拟机的问题
Système de gestion - itclub (II)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?
Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
Conversion between flat array and JSON tree
This set of steps for performance testing using JMeter includes two salary increases and one promotion
C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.
6G显卡显存不足出现CUDA Error:out of memory解决办法
美团20k软件测试工程师的经验分享
Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题
Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
Typescript learning
Which method is called for OSS upload
Is flush stock trading software reliable?? Is it safe?
Acwing week 57 longest continuous subsequence - (binary or tree array)
Software test automation test -- interface test from entry to proficiency, learn a little every day
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
管理系统-ITclub(上)
爬虫笔记(2)- 解析