JavaScript function definitions, function parameters and calls in the new Java introduction to architect tutorial

Posted by coffeehead on Thu, 02 Dec 2021 23:42:11 +0100

1, Definition of function

Functions, i.e. methods. It is a preset Function code block, which can be called repeatedly and return different values according to different input parameters. Functions are also objects. There are three methods of Function definition: Function declaration statement, Function definition expression and Function constructor.

1. Function declaration statement

function Function name([parameter list]){
    
}
For example:
function foo(){ 
    console.log(1);
} 
foo(); 

 

The function defined in this way has the effect of declaration promotion

foo();   
function foo(){ 
        console.log(1);
} 
// Variable declaration promotion
console.log( a );  
var a = 2;

 

2. Function definition expression

For functions defined in the form of expressions, the name of the function may not be required.

var Variable name = function ([parameter list]) {
    
}
Variable name();
For example:
var fun = function(){
    console.log("Hello");
}
fun();

 

This method assigns an anonymous function to a variable. At this time, this anonymous function is also called function expression, because only expression can be placed to the right of the equal sign of the assignment statement.

3. Function constructor

The Function constructor receives any number of parameters, but the last parameter is always regarded as the Function body, while the previous parameter lists the parameters of the new Function.

var add = new Function('x','y','return (x + y)');
// Equivalent to
function add(x, y) {
    return (x + y);
}
add();

 

Please note that:

1. Functions in JS are not overloaded. Functions with the same name will be overwritten by later functions.

2. js allows an indefinite number of parameters. The arguments object will be described later.

3. This article is the content of JavaScript function definition parameter call in the new Java big data training course document. If you need learning resources such as Java big data architect for the complete course, please write to me personally.

2, Parameters of function

When a function is running, it is sometimes necessary to provide external data. Different external data will get different results. This external data is called a parameter. The parameter at the time of definition is called a formal parameter, and the parameter at the time of call is called an argument.

  • If the argument can be omitted, the corresponding formal parameter is undefined
  • If the function parameter has the same name (generally not), the last value shall prevail.
  • Default values can be given to parameters: when parameters are special values, default values can be given.
  • Parameter is value transfer, and copy is transferred; When a reference is passed, the address is passed, and the same object is operated on.
// When calling a function, the argument can be omitted, and the corresponding formal parameter is undefined
function add(a , b) {
    console.log(a + "+" + b + "=" + (a + b));
}
add(3,4,5)//3+4=7
add(1);//1+undefined=NaN
add();//undefined+undefined=NaN

// If the function parameter has the same name (generally not), the last value shall prevail
function add2(a , a) {
    console.log(a);
}
add2(1,2);

// Default values for parameters
function defaultValue(a){
    a = a || "a";
    return a;
}
console.log(defaultValue());
function f(a){
    //If parameter a Not for undefined or null,Take its own value, otherwise give a default value
    (a !== undefined && a !== null) ? a = a : a = 1;
     return a;
}
console.log(f());

// pass by value
var num = 12;
function change(n) {
    n = 30;
}
change(num);
console.log(num);
// Reference passing
var obj = {name: "tom"};
function paramter(o) {
    o.name = 2;
}
paramter(obj);
console.log(obj.name);
// Give formal parameters o Given a new array
var obj2 = [1, 2, 3];
function paramter2(o){
    o = [2, 3, 4];
    o[1] = 3;
}
paramter2 (obj2);
console.log(obj2)

 

3, Function call

1. Common calling methods

Function name([Argument]);

If there is a return value, you can receive the variable. If you receive a function without a return value, it is undefined.

2. Function call mode

function add(a,b){
   return a+b;
} 
var sum = add(1,2) 
console.log(sum); 

 

3. Method call mode

var o = {
    m: function(){
         console.log(1); 
    } 
};
o.m();  

 

This article is the content of JavaScript function definition and parameter call in the document of the new Java big data training course. If you need learning resources such as Java big data architect for the complete course, please write to me personally.

Topics: Javascript function