//function of javascript //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. The parameters used by the method can be more or less than the specified number //2. The 'arguments' keyword is similar to an array, used to display the input parameters //Code 2: 'use strict' function func1(a, b, c) { if (arguments.length === 2) { c = b; b = null; } console.log(a + c); }; func1(1,2); //Code 2 explanation: //1. The 'arguments' keyword is used to determine the number of parameters passed in. The above //code turns parameter b into 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: //1. The ES6 standard introduces the 'rest' keyword to get all the 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 function of the above method sum(...rest) is to sum the input parameters //Code 5: 'use strict' function fun1() { where job = 'cook', desciption = ' cook delicious food'; console.log(job + desciption); }; fun1(); //Code 5 explanation: //1. Due to the existence of variable promotion, it is best for js functions to declare all variables at the starting position //Code 6: where a = 1, b = 2; [a, b] = [b, a]; console.log(a); console.log(b); //Application example 1: Swap the values of variables a and b var {hostname:domain, pathname:path} = location; //Application example 2: Get the domain name and path of the current page //Code 6 explanation: //1. Application of destructuring assignment //Code 7: function getAge() { var year = new Date().getFullYear(); return year - this.birthYear; } var li = { name: 'Li', birthYear: 1991, age: getAge } var ageLi = li.age (); console.log (ageLi); //27 ageLi = getAge (); console.log (ageLi); // NaN ageLi = getAge.apply(li, []); console.log (ageLi); //27 //Code 7 explanation: //1. Use the apply() method to change the point of this //Code 8: '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 function 'parseInt' of the 'window' object and let it have a counting function