Home >Web Front-end >JS Tutorial >JavaScript function calling and parameter passing_Basic knowledge

JavaScript function calling and parameter passing_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:35:591307browse

JavaScript function call
There are 4 ways to call JavaScript functions.
The way each differs is in the initialization of this.
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.
Call JavaScript function
The code within the function is executed after the function is called.
Call as a function
Example

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


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:
Example

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


Note This is a common way to call JavaScript functions, but it is not a good programming practice
Global variables, methods or functions can easily cause naming conflict bugs.
Global Object
When 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:
Example

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


Note: Calling a function as a global object will make the value of this a global object.
Using the window object as a variable can easily cause the program to crash.
Function called as method
In JavaScript you can define functions as methods of objects.
The following example creates an object (myObject) with two properties (firstName and lastName), and one method (fullName):
Example

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


The fullName 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 below! Modify the fullName method and return this value:
Example

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


Note: When a function is called as an object method, the value of this becomes the object itself.
Use constructor to call 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 recreated objects:
Example

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

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


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.
Call function as function method
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.
Example

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

Example

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


Both methods use the object itself as the first parameter. 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, the first parameter becomes the value of this when calling a function, even if the parameter is not an object.
In JavaScript non-strict mode, if the value of the first parameter is null or undefined, it will use the global object instead.
Note With the call() or apply() methods you can set the value of this and call it as a new method on an existing object.

JavaScript function parameters
JavaScript functions do not perform any checks on parameter values ​​(arguments).
Function explicit parameters and hidden parameters (arguments)
In previous tutorials, we have learned about explicit parameters of functions:

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


函数显式参数在函数定义时列出。
函数隐藏参数(arguments)在函数调用时传递给函数真正的值。
参数规则
JavaScript 函数定义时参数没有指定数据类型。
JavaScript 函数对隐藏参数(arguments)没有进行检测。
JavaScript 函数对隐藏参数(arguments)的个数没有进行检测。
默认参数
如果函数在调用时缺少参数,参数会默认设置为: undefined
有时这是可以接受的,但是建议最好为参数设置一个默认值:
实例

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

或者,更简单的方式:
实例

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

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;
}

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

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;
}


通过值传递参数
在函数中调用的参数是函数的参数。
如果函数修改参数的值,将不会修改参数的初始值(在函数外定义)。
函数参数的改变不会影响函数外部的变量(局部变量)。
通过对象传递参数
在JavaScript中,可以引用对象的值。
因此我们在函数内部修改对象的属性就会修改其初始的值。
修改对象属性可作用于函数外部(全局变量)。

Statement:
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