Home > Web Front-end > JS Tutorial > Detailed explanation of JavaScript function calling and parameter passing usage examples

Detailed explanation of JavaScript function calling and parameter passing usage examples

伊谢尔伦
Release: 2017-07-26 17:30:51
Original
4250 people have browsed it

JavaScript function call

The difference between each method lies in the initialization of this.

Pass parameters by value
The parameters called in the function are the parameters of the function. If a function modifies the value of a parameter, it will not modify the parameter's initial value (defined outside the function). Changes to function parameters will not affect variables outside the function (local variables).
Passing parameters through objects
In JavaScript, you can reference the value of an object. Therefore, when we modify the properties of the object inside the function, its initial value will be modified. Modifying object properties can act outside the function (global variables).

this Keyword

Generally speaking, in Javascript, this points to the current object when the function is executed. Note Note that this is a reserved keyword, you cannot modify the value of this.

Calling JavaScript functions

The code in the function is executed after the function is called.

Called as a function

Instance


##

function myFunction(a, b) {
  return a * b;
}
myFunction(10, 2);      // myFunction(10, 2) 返回 20
Copy after login

The above function does not belong to any object. But in JavaScript it is always the default global object. The default global object in HTML is the HTML page itself, so the function belongs to the HTML page. The page object in the browser is the browser window (window object). The above functions will automatically become functions of the window object. myFunction() and window.myFunction() are the same:



function myFunction(a, b) {
  return a * b;
}
window.myFunction(10, 2);  // window.myFunction(10, 2) 返回 20
Copy after login

Note This is a common method for calling JavaScript functions, but it is not a good programming habit. Or functions can easily cause bugs in naming conflicts.


Global objectWhen the function is not called by its own object, the value of this will become the global object. In a web browser the global object is the browser window (window object). The value of this returned by this instance is the window object:


function myFunction() {
  return this;
}
myFunction();        // 返回 window 对象
Copy after login

Note If the function is called as a global object, the value of this will become the global object. Using the window object as a variable can easily cause the program to crash.

Function calls as methods

In JavaScript you can define functions as methods of objects. The following example creates an object (myObject), which has two properties (firstName and lastName), and one method (fullName):



var myObject = {
  firstName:"John",
  lastName: "Doe",
  fullName: function () {
    return this.firstName + " " + this.lastName;
  }
}
myObject.fullName();     // 返回 "John Doe"
Copy after login

fullName The method is a function. Functions belong to objects. myObject is the owner of the function.

This object holds JavaScript code. The value of this in the instance is the myObject object. Test the following! Modify the fullName method and return this value:


var myObject = {
  firstName:"John",
  lastName: "Doe",
  fullName: function () {
    return this;
  }
}
myObject.fullName();     // 返回 [object Object] (所有者对象)
Copy after login

Note When the function is called as an object method, the value of this will become the object itself.

Use the constructor to call the function

If the new keyword is used before the function call, the constructor is called.

This looks like a new function is created, but in fact JavaScript functions are re-created objects:


// 构造函数:
function myFunction(arg1, arg2) {
  this.firstName = arg1;
  this.lastName = arg2;
}

// This creates a new object
var x = new myFunction("John","Doe");
x.firstName;               // 返回 "John"
Copy after login

The call to the constructor creates a new object. The new object inherits the constructor's properties and methods. Note The this keyword in the constructor does not have any value. The value of this is created when the object (new object) is instantiated when the function is called.

Calling functions as function methods

In JavaScript, functions are objects. A JavaScript function has its properties and methods.

call() and apply() are predefined function methods. Two methods can be used to call functions, and the first parameter of both methods must be the object itself.


function myFunction(a, b) {
  return a * b;
}
myFunction.call(myObject, 10, 2);   // 返回 20
Copy after login

Instance



function myFunction(a, b) {
  return a * b;
}
myArray = [10,2];
myFunction.apply(myObject, myArray);  // 返回 20
Copy after login

Both methods use the object itself as the first parameters. The difference between the two lies in the second parameter: apply passes in a parameter array, that is, multiple parameters are combined into an array and passed in, while call is passed in as the parameter of call (starting from the second parameter).

In JavaScript strict mode (strict mode), the first parameter will become the value of this when calling the function, even if the parameter is not an object.
In JavaScript non-strict mode (non-strict mode), if the value of the first parameter is null or undefined, it will use the global object instead.
Note Through the call() or apply() method you can set the value of this and call it as a new method of an existing object.

JavaScript function parameters

JavaScript functions do not perform any checks on the values ​​of parameters (arguments).

Function explicit parameters and hidden parameters (arguments)In the previous tutorial, we have learned about the explicit parameters of the function:


functionName(parameter1, parameter2, parameter3) {
  code to be executed
}
Copy after login

Explicit parameters of functions are listed when the function is defined.

Function hidden parameters (arguments) are the real values ​​passed to the function when the function is called.

参数规则
JavaScript 函数定义时参数没有指定数据类型。
JavaScript 函数对隐藏参数(arguments)没有进行检测。
JavaScript 函数对隐藏参数(arguments)的个数没有进行检测。
默认参数
如果函数在调用时缺少参数,参数会默认设置为: undefined

有时这是可以接受的,但是建议最好为参数设置一个默认值:


function myFunction(x, y) {
  if (y === undefined) {
     y = 0;
  } 
}
Copy after login

或者,更简单的方式:


function myFunction(x, y) {
  y = y || 0;
}
Copy after login

Note 如果y已经定义 , y || 返回 y, 因为 y 是 true, 否则返回 0, 因为 undefined 为 false。如果函数调用时设置了过多的参数,参数将无法被引用,因为无法找到对应的参数名。 只能使用 arguments 对象来调用。

Arguments 对象
JavaScript 函数有个内置的对象 arguments 对象.argument 对象包含了函数调用的参数数组。通过这种方式你可以很方便的找到最后一个参数的值:


x = findMax(1, 123, 500, 115, 44, 88);

function findMax() {
  var i, max = 0;
  for (i = 0; i < arguments.length; i++) {
    if (arguments[i] > max) {
      max = arguments[i];
    }
  }
  return max;
}
Copy after login

或者创建一个函数用来统计所有数值的和:


x = sumAll(1, 123, 500, 115, 44, 88);

function sumAll() {
  var i, sum = 0;
  for (i = 0; i < arguments.length; i++) {
    sum += arguments[i];
  }
  return sum;
}
Copy after login

The above is the detailed content of Detailed explanation of JavaScript function calling and parameter passing usage examples. For more information, please follow other related articles on the PHP Chinese website!

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