Home > Web Front-end > JS Tutorial > body text

Parameters of Javascript functions

PHPz
Release: 2018-09-29 15:27:32
Original
1315 people have browsed it

This chapter will introduce you to the parameters of Javascript functions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Parameters of function 

For parameter values, JavaScript will not perform type checking, and any type of value can be passed to the parameter.
For function parameters, if there are too few, the parameter value that does not get a value will be undefined. If there are too many, the extra parameters will be ignored.

  //调用函数时传入的实际参数
  function add(num1, num2) {
    return num1 + num2;
  }
  //一切正常的调用方式
  console.info(add(1, 2)); //3
  //不检查参数的类型,字串和数字可以混用
  console.info(add("1", 2)); //12
  //多余参数被忽略
  console.info(add(1, 2, 3)); //3
  //少的参数被视为undefined
  //1+undefined=NaN
  console.info(add(1)); //NaN
Copy after login

Detect whether the parameter is missing
Judge whether it is undefined

  //检测参数是否缺失
  function sayHello(name, message) {
    if (typeof message === 'undefined') {
      message = '你好!';
    }
    console.info(name + "," + message);
  }
  sayHello("贾君鹏", "你妈喊你吃饭"); 
  sayHello("贾君鹏");    //贾君鹏,你好!
Copy after login

arguments object to save the parameter

Use the arguments object to write a function that supports any number of parameters. arguments looks like an array, but is not actually an array, so many array methods cannot be used.

  //保存参数的arguments对象
  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
Copy after login

The length attribute of the function object

arguments.length The number of actual parameters received by the function
Function name .length Function definition formal parameters

 //函数对象的length属性
 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
 console.info(sayHi.length); //0
Copy after login

Function object as parameter
A function is an object that can be used as a parameter of another function

//作为参数的函数对象
 function callSomeFunction(func, argu) {
   return func(argu);
 }
 function getGreeting(name) {
   return "Hello, " + name;
 }
 var result = callSomeFunction(getGreeting, "jxl");
 console.info(result); //Hello,jxl
Copy after login

Use named/anonymous functions as function parameters

  var callTwice = function (otherFunc) {
    otherFunc();
    otherFunc();
  };
  callTwice(function () {
    console.info("this is a function");
  });
Copy after login

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template