当前位置:网站首页>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
边栏推荐
- 【mysql学习08】
- 矩阵的特征值和特征向量
- [MySQL learning 09]
- 用 Redis 做一个可靠的延迟队列
- 基于W5500实现的考勤系统
- 谣言检测文献阅读十一—Preventing rumor spread with deep learning
- Menu bar + status bar + toolbar ==pyqt5
- Maskgae: masked graph modeling meets graph autoencoders
- Teach you how to configure S2E to UDP working mode through MCU
- Teach you how to configure S2E as the working mode of TCP server through MCU
猜你喜欢

JVM performance tuning methods

谣言检测文献阅读十一—Preventing rumor spread with deep learning

Onenet platform control w5500 development board LED light

How to judge the performance of static code quality analysis tools? These five factors must be considered

Understanding: idea uses Scala to write wordcount programs and generate jar packages

Learn NLP with Transformer (Chapter 1)

城市雕塑典型作品信息管理系统(图片分享系统SSM)

Chapter 4 linear equations

Review in the middle of 2022 | understand the latest progress of pre training model

Breadth first traversal (problems related to sequence traversal of graphs and binary trees)
随机推荐
数据库完整性——六大约束学习
Shell - Chapter 8 exercise
基于MATLAB的常见线性调制方法
JVM性能调优方法
SQL injection less23 (filter comment)
Shell - Chapter 6 exercise
Greedy problem 01_ Activity arrangement code analysis
W5500 is in TCP_ In server mode, you cannot Ping or communicate in the switch / router network.
[MySQL learning 08]
矩阵的特征值和特征向量
基于W5500实现的考勤系统
将字符串转换为数字
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
SQL language (III)
Linked list related (design linked list and ring linked list)
Filter过滤器解决request请求参数乱码的原理解析
Teach you how to configure S2E as the working mode of TCP server through MCU
软件测试阶段的风险
谣言检测文献阅读十一—Preventing rumor spread with deep learning
The most efficient note taking method in the world (change your old version of note taking method)