This article will share with you the course notes of CICC xuliang In the Netease cloud class, which is very helpful for you to learn javascript. Here we recommend this article to your friends. This article is the course notes of the Netease cloud class by Jin xuliang, record down for memo
Function Parameters
For parameter values, JavaScript does not check the type, and any type value can be passed to the parameter.
If there are too few function parameters and the value of the parameter is undefined, too many parameters will be ignored.
// The actual Parameter function add (num1, num2) {return num1 + num2;} // all normal call Methods: lele.info (add (1, 2 )); // 3 // The parameter type is not checked. Strings and numbers can be mixed with lele.info (add ("1", 2 )); // 12 // extra parameters are ignored lele.info (add (1, 2, 3 )); // 3 // few parameters are considered as undefined // 1 + undefined = NaN console.info (add (1); // NaN
Check whether the parameter is missing
Determine whether it is undefined
// Check whether the function sayHello (name, message) {if (typeof message = 'undefined') {message = 'Hello! ';} Lele.info (name + "," + message);} sayHello ("Jia Junpeng", "Your mom calls you dinner"); sayHello ("Jia Junpeng "); // hello, Jia Junpeng!
Save the parameter's arguments object
Use the arguments object to write functions that support any parameter. Arguments is like an array, but it is not actually an array, so many array methods are not available.
// Save the parameter's arguments object function sumNumbers () {var result = 0; for (var I = 0; I <arguments. length; I ++) {result + = arguments [I];} return result;} console.info (sumNumbers (1, 2); // 3 console.info (sumNumbers (1, 2, 3); // 6
The length attribute of the function object.
Number of arguments received by the arguments. length Function
Function name. length parameter defined by the function
// Function object length attribute function sayName (name) {console.info (name);} function sum (num1, num2) {return num1 + num2;} function sayHi () {console.info ("hi");} console.info (sayName. length); // 1 console.info (sum. length); // 2 lele.info (sayHi. length); // 0
Function object as a parameter
A function is an object that can be used as a parameter of another function.
// Function object callSomeFunction (func, argu) {return func (argu);} function getGreeting (name) {return "Hello," + name ;} var result = callSomeFunction (getGreeting, "jxl"); console.info (result); // Hello, jxl
Use name/anonymous functions as function parameters
var callTwice = function (otherFunc) { otherFunc(); otherFunc(); }; callTwice(function () { console.info("this is a function"); });
Function as a parameter instance:
《script》 var button = document.getElementById('btnClick'); var result = document.getElementById('result'); var clickCount = 0; button.addEventListener('click', function () { clickCount++; result.setAttribute('value', clickCount+': Hello,world!'); })《script》
The above is all the content of this article. I hope you will like it.