JavaScript functions//code 1: ' Use strict ' function foo (x) {console.log (' x= ' + x); for (var i = 0; i < arguments.length; i + +) {console.log (' arguments[' + i + ']= ' + arguments[i]); }};foo (1,2,3,4,5);//code 1 Explanation://1. Method can use more or less than the specified number of//2. ' Arguments ' keyword-like array, used to display input parameters//code 2: ' Use strict ' function func1 (A, B, c) {if (arguments.length = = = 2) {c = b; b = null; } console.log (A + c);}; func1;//Code 2 Explanation://1. ' Arguments ' keyword is used to determine the number of parameters passed in, the above//code changes the parameter B to a variable parameter//code 3: ' Use strict ' function func1 (A, B, ... rest) {console.log (a); Console.log (b); Console.log (rest);}; Func1 (1,2,3,4,5);//1//2//[3,4,5]func1 (1);//1//undefined//[]//Code 3 Explanation: The//1.ES6 standard introduces the ' rest ' keyword to get all remaining parameters//code 4: ' Use Strict ' function sum (... rest) {var sum = 0; for (var i = 0; i < rest.length; i + +) {sum + = Rest[i]; } return sum;}; var result = SUM (1,2,3,4,5), Console.log (Result),//15//Code 4 Explanation://1. The above method sum (... rest) function is to sum the input parameters//code 5: ' Use strict ' function fun1 () {var job = ' Cook ', desciption = ' Cook delicious food '; Console.log (Job + desciption);}; FUN1 ();//Code 5 Explanation://1. Because of the existence of variable promotion, the JS function is best to declare all variables//code 6:vara = 1,b = 2 in the starting position; [A, b] = [B, A];console.log (a); Console.log (b);//Application Example 1: Exchange variables A and B values var {hostname:domain, Pathname:path} = location;// Application Example 2: Get the current page's domain name and path//code 6 Explanation://1. Application of Deconstruction Assignment//code 7:function Getage () {var year = new Date (). getFullYear (); return year-this.birthyear;} var li = {name: ' Lee ', birthyear:1991, Age:getage}var Ageli = Li.age (); Console.log (Ageli);//27ageli = Getage (); c Onsole.log (Ageli);//nanageli = Getage.apply (Li, []); Console.log (Ageli);//27//Code 7 Explanation://1. Use the Apply () method to change the point//code 8 for this: ' Use strict ' var count = 0;var Oldparseint = Parseint;window.parseint = function () {count + = 1; Oldparseint.apply (null, arguments);} for (var i = 0; i < 5; i + +) {parseint (' 19 ');} Console.log (count);//code 8 Explanation://1. Replace the ' window ' object's function ' parseint ' to have a count function
Functions of JavaScript