Higher-level usage of the function
- There are three common ways of asynchrony: timer functions, event click functions, and Ajax asynchronous requests
- All functions are instances of Function
- Several ways to define functions
-
Function fn(){}// Named function var fn = function(){} // Anonymous function var fn = new function() // The argument is a stringCopy the code
- Function call
- Var fn = function(){} fn() calls fn.call() can also be called
- Calls to object functions (also called methods)
-
Var o = {say: function () {conlog. Log (' singing ')}} grown ay () / / callsCopy the code
-
- Constructor call
-
Function Star(){} call: new Star();Copy the code
-
- The call to the event-binding function
- Btn.onclick = function(){
- Timer function call, how often to call
- Execute the function call immediately
- (function(){})(); Direct call
- The call method is used for function calls. The core usage is to change the direction of this. It is usually used for child constructor inheritance
- The apply method is called with an array, such as the maximum value in the array
-
Var arr = [1,2,4,6,87] var Max = math.max. Apply (Math,arr)//Math this points to conlog.log(Max)//87 the maximum value in the array is displayed as a stringCopy the code
-
- The bind method, which takes a string, changes this, but does not execute the function immediately. It is used for timer operations
-
<! DOCTYPE HTML >< HTML >< head> <title></title> <button> button </head> <body> <script type="text/javascript"> var BTN = document.querySelector('button') btn.onclick = function(){ this.disabled = true; SetTimeout (function(){this.disabled = false}.bind(this),3000)} </script> </body>Copy the code
-
- Js strict mode?
- Add ‘use strict’ at the beginning of the code; You can start in strict mode
- Strict mode can only be recognized in IE10 or later. Strict mode can be used to add whole JS scripts as well as specific functions
- In strict mode, variables cannot be assigned directly, they must be defined first, and cannot be deleted by using del. Variables must be declared first
- Strictly schema-defined functions this no longer refer to window, and undefind.
- In the constructor, the function must be called using the new keyword
- The timer function “this” refers to the global window
- Functions cannot be defined in non-code blocks, such as for loops,if conditions cannot write functions, and functions can be nested within functions
- Variables cannot be repeated in strict mode
-
- Higher order functions? A function whose arguments are callbacks is called a higher-order function
- Closure? One function scope accesses a local variable in another function, and that creates a closure, right? You could also say closures are a phenomenon
- What do closures do? You can extend the scope of variables, and return is the primary keyword for closures
-
function fn(){ var num = 10; return function(){ console.log(num) } } var f = fn(); f(); // The function of closuresCopy the code
- You can use recursion to call yourself when you go through the second level of the array
- Deep copy and shallow copy
- A shallow copy only copies the memory address, such as an object. If you change the value of the object, both will change
- In ES6, an Object method was added to solve the shallow copy problem object.assign ()
- Parameter 1: the object to copy
- Parameter 2: To whom to copy
- In ES6, an Object method was added to solve the shallow copy problem object.assign ()
- A shallow copy only copies the memory address, such as an object. If you change the value of the object, both will change
- The second definition format of regular expressions is generally used
-
var reg = new new RegExp(/123/); var reg = /123/; Copy the code
- ^ Indicates the beginning, $indicates the end, and [a-z] indicates any letter from a to Z
- Character limit: any character [0-9A-zA-z] can be added to the brackets
- ^ outside [] ^[] indicates where to start, and ^ inside [] indicates the inverse [^0-9] indicates that no numbers are allowed
-
- Indicates >=0, that is, 0 or multiple occurrences can occur
- The + sign indicates >=1, which can occur once or more times
- ? It’s either 1 or 0
- {3} indicates three times, {3,} indicates more than three times >=3, {3, 16} indicates >=3 and <=16 times
- The expression is followed by a modifier
- \d===[0-9] represents any number,\ d===[^0-9] represents all but numbers
- \w===[0-9a-za-z-_] indicates any alphanumeric lowercase underscore \w indicates the reverse
- \s===[\t\r\n\f\v] indicates any space, including newline characters, TAB characters, Spaces, etc. \W indicates the inverse
- The new use of regular expressions replace
- Replace takes two arguments
- Parameter 1: A string or regular expression (old value)
- Parameter 2: The new value to be replaced
- The regular expression can have arguments /123/g for global I for case-insensitive gi for national and case-insensitive
-