This article mainly introduces JavaScriptrelated information on defining functions. Friends who need it can refer to it
In JavaScript, the way to define functions is as follows:
function abs(x) { if (x >= 0) { return x; } else { return -x; } }
The above abs() function is defined as follows:
function indicates that this is a function definition;
abs is the name of the function;
(x) The parameters of the function are listed in brackets , multiple parameters are preceded by, Separation; The code between
conditional judgment and loops.
If there is no return statement, the result will be returned after the function is executed, but the result will be undefined. Since a JavaScript function is also an object, the abs() function defined above is actually a function object, and the function name abs can be regarded as a variable pointing to the function. So, the second way of defining a function is as follows:var abs = function (x) { if (x >= 0) { return x; } else { return -x; } };
Anonymous function, it has no function name. However, this anonymous function is assigned to the variable abs, so the function can be called through the variable abs.
The above two definitions are completely equivalent. Note that the second method requires adding a ; at the end of the function body according to the complete syntax, indicating the end of the assignment statement.Call the function
When calling the function, just pass in the parameters in order:
abs(10); // 返回10 abs(-9); // 返回9
abs(10, 'blablabla'); // 返回10 abs(-9, 'haha', 'hehe', null); // 返回9
abs(); // 返回NaN
function abs(x) { if (typeof x !== 'number') { throw 'Not a number'; } if (x >= 0) { return x; } else { return -x; } }
arguments
JavaScript also has a free keyword arguments, which only works inside the function and always points to all parameters passed in by the caller of the current function. arguments is similar to Array but it is not an Array:function foo(x) { alert(x); // 10 for (var i=0; i<arguments.length; i++) { alert(arguments[i]); // 10, 20, 30 } } foo(10, 20, 30);
function abs() { if (arguments.length === 0) { return 0; } var x = arguments[0]; return x >= 0 ? x : -x; } abs(); // 0 abs(10); // 10 abs(-9); // 9
// foo(a[, b], c) // 接收2~3个参数,b是可选参数,如果只传2个参数,b默认为null: function foo(a, b, c) { if (arguments.length === 2) { // 实际拿到的参数是a和b,c为undefined c = b; // 把b赋给c b = null; // b变为默认值 } // ... }
rest parameters
Since JavaScript functions allow receiving any number of parameters, we have to use arguments to obtain all parameters:function foo(a, b) { var i, rest = []; if (arguments.length > 2) { for (i = 2; i<arguments.length; i++) { rest.push(arguments[i]); } } console.log('a = ' + a); console.log('b = ' + b); console.log(rest); }
function foo(a, b, ...rest) { console.log('a = ' + a); console.log('b = ' + b); console.log(rest); } foo(1, 2, 3, 4, 5); // 结果: // a = 1 // b = 2 // Array [ 3, 4, 5 ] foo(1); // 结果: // a = 1 // b = undefined // Array []
'use strict'; // 测试: var i, args = []; for (i=1; i<=100; i++) { args.push(i); } if (sum() !== 0) { alert('测试失败: sum() = ' + sum()); } else if (sum(1) !== 1) { alert('测试失败: sum(1) = ' + sum(1)); } else if (sum(2, 3) !== 5) { alert('测试失败: sum(2, 3) = ' + sum(2, 3)); } else if (sum.apply(null, args) !== 5050) { alert('测试失败: sum(1, 2, 3, ..., 100) = ' + sum.apply(null, args)); } else { alert('测试通过!'); }
Be careful with your return statement
We mentioned earlier that the JavaScript engine has a mechanism to automatically add a semicolon at the end of the line, which may cause you to fall into a big pitfall with the return statement:function foo() { return { name: 'foo' }; } foo(); // { name: 'foo' }
function foo() { return { name: 'foo' }; } foo(); // undefined
function foo() { return; // 自动添加了分号,相当于return undefined; { name: 'foo' }; // 这行语句已经没法执行到了 }
function foo() { return { // 这里不会自动加分号,因为{表示语句尚未结束 name: 'foo' };
练习
定义一个计算圆面积的函数area_of_circle(),它有两个参数:
r: 表示圆的半径;
pi: 表示π的值,如果不传,则默认3.14
The above is the detailed content of Introduction to how to define functions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!