当前位置:网站首页>Classification parameter stack of JS common built-in object data types
Classification parameter stack of JS common built-in object data types
2022-07-25 11:47:00 【c_ perfectworld】
Classification of objects
- JS The objects in are divided into : Custom object 、 Built-in objects 、 Browser object
- The first two objects are JS Basic content , Belong to ECMAscript, The third browser object is JS alone possess Of
- Built-in objects yes JS Some self-contained objects , For developers , Provide common or basic properties and methods
- The biggest advantage of built-in objects is that they help us develop quickly
- JS Provide more to built-in objects :Math、Date、Array、String etc.
MDN Use of documents
- Check the function of this method
- Check the meaning and type of parameters inside
- Check the meaning and type of the return value
- adopt demo To test
Math object
Not a constructor , So no need new To call , Instead, you can directly use the properties and methods inside
- Math.PI // PI
- Math.floor() // Rounding down
- Math.ceil() // Rounding up
- Math.round () // Round the page Round to the nearest -3.5 The result is -3
- Math.abs() // The absolute value
- Math.max() Math.min() // Maximum and minimum
Math Object random number method
- Math.random() Returns a random decimal , The scope is [ 0 , 1 ) Between , from 0 Start ( Include 0) To 1( barring 1)
- There are no parameters in this method
Date Date object
It's a constructor , Must use new Call create date object
- If there are no parameters , Returns the current time of the current system
- Parameters are commonly written Digital 2019,10,01 ( The return is 11 Month is not 10 month ) String type ‘2019 - 10 - 1 8:8:8’
- Used to handle dates and times
Date formatting
getMonth() The returned month is small 1 Months , need +1
getDay() The return on Sunday is 0
Get the total number of milliseconds of the date
- valueOf()
- getTime()
- Simple writing var date1 = +newDate() + newDate The total number of milliseconds is returned ( The most commonly used )
- console.log(Date.now()) H5 newly added
Time stamp , Never repeat
Countdown case analysis
- Subtract the present time from the future time
- Use time stamps to do , The total milliseconds of user input time minus the total milliseconds of current time
- Convert the remaining total milliseconds into days 、 when 、 branch 、 second
Array Array objects
Two ways : Literal [ ] new keyword
var arr1 = newArray(2) // 2 Represents the length of an array , There are two empty array elements var arr1 = newArray(2,3) amount to var arr1 = [2,3] Indicates that there are two array elements 2 Follow 3Check if it's an array
- instanceOf Operator It can be used to detect whether it is an array
- Array.isArray( Parameters )
| Method name | explain | Return value |
| push( Parameters 1) | Add one or more elements at the end , Pay attention to modifying the original array | And returns the new length |
| pop() | Returns the value of the element it deleted | |
| unshift( Parameters 1) | Add one or more elements to the beginning of an array , Pay attention to modifying the original array | And returns the new length |
| shift() | Deletes the first element of the array , Array length minus 1 No parameter 、 Modify the original array | And returns the value of the first element |
push
- push Yes, you can add new elements to the array
- push() Parameters write array elements directly
- push After the completion of , The return result is the length of the new array
- The original array changes
unshift
- unshift('blue'); // Add... At the front , The returned number is the group leader
- The original array changes
pop
- pop() Delete the last element What is returned is the deleted element
- () There are no parameters in it
shift
- shift() Delete first element What is returned is the deleted element
- () There are no parameters in it
Get array element index indexOf str.indexOf(‘ Characters to find ’,[ Starting position ])
Be careful : It is often used to judge whether an element is in an array !! return -1 Indicates that it is not in the array
Array conversion string join toString()
Flip array :arr.reverse()
Bubble sort :arr.sort()
String object
Basic packaging type : Is to put simple data types , Packaging becomes a complex data type
var str = ('andy')
amount to
(1) Wrap simple data types into complex data types
var temp = newString('andy')
(2) Give the value of the temporary variable to str
str = temp
(3) Destroy this temporary variable
temp = nullThe immutability of string
Means that the value inside is immutable , Although it looks different , In fact, the address has changed , Created a new memory space
Don't go ahead A lot of splicing character string , It's easy to cause computer jam
String method
The string itself is not modified , When the operation is completed, a new string is returned
- Return position according to character
indexOf( Characters to find , Starting position ): return Specify content Position in the meta string , Can't find return -1 , The starting position is index Reference no. indexOf( To find the character [ The starting position ])
lastindexOf() From back to front look for , Only look for The first match Of
- Returns the character according to the position
| Method name | explain | Use |
|---|---|---|
| charAt(index) | Returns the specified position character (index String index ) | str.charAt(0) |
| charCodeAt(index) | Gets the character of the specified position ASCLL code ( Reference no. ) | str.charCodeAt(index) |
| str [index] | Gets the character at the specified location | H5,IE8+ Support |
- Operation method of string
| Method name | explain |
|---|---|
| concat(str1,str2,str3...) | concat() Method to connect two or more strings . String concatenation , Equivalent to + |
| substr(start,length) | from start Position start ( Reference no. ),length Take the number of |
| slice(start,end) | from start Position start , Intercept to end Location ,end Can't get ( Both are index numbers ) |
| substring(start,end) | from start Start to intercept to end,end Can't get , Basic and slice Agreement , Negative values are not acceptable |
- Replace character replace(‘ Replaced characters ’,‘ The replaced character ’) It will only replace the first character
- Character to array split(‘ Separator ’)
- Convert a capital toUpperCase()
- Convert lowercase toLowerCase()
JS Simple data types and complex data types
Simple data type is also called basic data type or Value type , Complex data types are also called Reference type
- Value type : The value stored in the variable during storage is the value itself
string、number、boolean、undefined、null A special ( Returns an empty object )
If you want to store a variable as an object in the future , I haven't figured out what to put , And save it as null
- Reference type : When storing variables, only the address is stored in the variables ( quote ), adopt new Keyword created objects ( System object 、 Custom object ) Such as :object、Array、Date
Heap and stack
Differences in stack space allocation :
- Stack ( operating system ): The operating system automatically assigns the parameter value of the storage function 、 The value of a local variable . The operation mode is similar to the stack in the data structure Simple data types are stored on the stack
- Pile up ( operating system ): Store complex types ( object ), Release is assigned by the programmer , If programmers don't release , Recycling by garbage collection mechanism Complex data types are stored in the heap
Be careful :JS There is no concept of stack , By stack , It is easier to understand how the code is executed
- Simple data type Open directly in the stack A space storage value
- Complex data types are stored in the stack first Hexadecimal address , Then this address Point to the heap The data of , Values of truly complex data types All stored in the pile Inside
Parameter transfer of data type
Parameters of simple data types
Function parameter can be regarded as a variable , When one Value type variables are passed as parameters to the formal parameters of the function when , In fact, it is the value of the variable in the stack space Copy A formal parameter to the function , Then any modification of formal parameters inside the function is Does not affect external value variables
Parameters of complex data types
A function parameter can also be regarded as a variable , When passing a reference type variable to a formal parameter , In fact, it is the heap address of the variable in the stack space Copy the One for the formal parameters , Formal parameters and actual parameters The same address is saved , therefore Operate on the same object
边栏推荐
猜你喜欢

Attendance system based on w5500

Leetcode sword finger offer 28. symmetric binary tree

【mysql学习08】

Teach you how to configure S2E to UDP working mode through MCU

Common web attacks and defense

阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

小微企业智能名片管理小程序

OneNET平台控制W5500开发板LED灯

论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
随机推荐
W5500 is in TCP_ In server mode, you cannot Ping or communicate in the switch / router network.
Summary of combination problems of Li Kou brush questions (backtracking)
大话DevOps监控,团队如何选择监控工具?
The first C language program (starting from Hello World)
ESP8266 使用 DRV8833驱动板驱动N20电机
W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
Shell Chapter 7 exercise
圆角大杀器,使用滤镜构建圆角及波浪效果!
Small program of vegetable distribution in community
Txt to CSV file, blank lines appear every other line
Layout management ==pyqt5
Teach you how to configure S2E to UDP working mode through MCU
【MySQL 17】安装异常:Could not open file ‘/var/log/mysql/mysqld.log‘ for error logging: Permission denied
如何判断静态代码质量分析工具的性能?这五大因素必须考虑
小微企业智能名片管理小程序
第一个C语言程序(从Hello World开始)
[electronic device notes 5] diode parameters and selection
Eigenvalues and eigenvectors of matrices
阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程
[dynamic planning] 70. Climbing stairs