JavaScript function detailed introduction

JavaScript function detailed introduction

Any number of statements can be encapsulated through functions and can be called and executed anywhere and at any time.

Our JavaScript scripting language is quite special. Compared with C language, its parameters do not require data type support. I won't describe the return value in detail. It is the same as the C language. If you don't write it, it will automatically return undefined

function fun(x,y){
            
        }
//Write this to declare a function

As far as I understand, he is passing in parameters in the form of objects, and using the various attribute values ​​of the object (values ​​of reference type) as my actual parameters.

For example, I have the following approach:

function fun(x, y) {
          // alert("The value of x is " + x.value);
            alert("The value of x is " + x);
        }

When I do this, the pop-up box reports an error: The value of x is [object HTMLInputElement], which is passed in as an object and does not conform to my logical design. It should be done as follows.

Returns my value of x.

function fun(x, y) {
            alert("The value of x is " + x.value);
          // alert("The value of x is " + x);
        }

  • So when I design a simple, two integer numbers multiplied, return the value.
  • Here you need to remember that <form></form> should be used to write the input html code in this tag. Otherwise you will spend a lot of time looking for errors (don’t ask me how I know, it’s just tears).

We have a deep understanding, don't worry, let's take a look at this code:

Is there something different here? According to the previous way of writing this function, it must be written randomly, right? But it works here, so funny (laughing happily)

That is to say, ECMAScript function does not mind how many parameters are passed in, nor does it care what parameters you pass in, and the parser will never have any complaints. (That’s great! The teacher no longer has to worry about me not knowing how to pass parameters.) So how did he solve this “problem of the century”? In fact, in each function body, there is an arguments object to access this parameter array, so as to obtain each parameter passed to the array.

Fellow Taoists, try to use double quotes for the following code fun function fun ("Brave", "Niu Niu"), what will happen? The explanation is that the upper quotes are paired with the nearest ones, woo woo woo

Hey, isn’t this a bit like the java function overloading we learned? In fact, JavaScript does not have overloading.

Let’s look at two more examples:

function fun() {
    if (arguments.length == 1) {
        alert(arguments[0] + 10);

    }
    if (arguments.length == 2) {
        alert(arguments[0] + arguments[1]);
    }
}
fun(10);//20
fun(10,20);//30

It can only achieve appropriate functions by passing in the number of parameters, and it does not achieve real overloading. Although the feature is not a perfect overload, it is enough to make up for this regret of JavaScript . arguments can also be used with parameters.

as follows:

function fun(num1, num2) {
    if (arguments.length == 1) {
        alert(num1 + 10);

    }
    if (arguments.length == 2) {
        alert(arguments[0] + num2);
    }

}
fun(10);//20
fun(10, 20);//30

Then someone asked, the parameters are assigned to the arguments object anyway, so is it possible for me to directly modify the value of arguments[i] ?

  • The interesting thing is that regarding the behavior of arguments , its value is always synchronized with the corresponding named parameter value, and it will modify the value in the current function! ! ! , which means that the parameters and arguments[i] memory space are independent, but the values ​​are synchronized.
function fun(num1, num2) {
    arguments[1] = 10;
    alert(arguments[0] + num2);
}

</script>
<form>
 <br><input type="button" onclick="fun(10,20)" value="click">
</form>

The value in the pop-up box is 20. This shows that modifying the value of arguments[i] will automatically reflect this parameter. If this parameter does not exist, rewriting this parameter value will result in a syntax error. The code will not execute. It will not complain if there are two identical function names, but the name will only belong to the function defined later.

Summarize:

  • JavaScript functions differ from functions in other languages ​​in many subtle ways.
  • No need to return a value, because it can return any value at any time
  • arguments.length is determined by the number of arguments passed in, not the number of names given to the function when it is defined.
  • There is no overloading, that is, the parameters can be participated in the form of 0 or more arrays, and these parameters are accessed through the arguments object.

This is the end of this article about the detailed introduction of JavaScript function . For more relevant JavaScript function content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of the use and precautions of javascript function (function type)
  • Analysis of the usage of JavaScript functional programming (Functional Programming) composition function (Composition)
  • Analysis of the usage of arrow functions in JavaScript functional programming
  • Analysis of the usage of higher order functions in JavaScript functional programming
  • Analysis of pure function usage in JavaScript functional programming
  • JavaScript Functional Programming: Declarative and Imperative Example Analysis
  • Inject eval, Function and other system functions into JS to intercept dynamic code
  • Talk about the use of functional components in Vue.js

<<:  12 Javascript table controls (DataGrid) are sorted out

>>:  Example of Form action and onSubmit

Recommend

Detailed explanation of how to use the calendar plugin implemented in Vue.js

The function to be implemented today is the follo...

Complete steps to build a squid proxy server in linux

Preface This article mainly introduces the releva...

HTML left, center, right adaptive layout (using calc css expression)

In the latest HTML standard, there is a calc CSS e...

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...

Vue computed properties

Table of contents 1. Basic Examples 2. Computed p...

The complete version of the common Linux tool vi/vim

Why learn vim Linux has a large number of configu...

Several ways to add timestamps in MySQL tables

Scenario: The data in a table needs to be synchro...

How to import, register and use components in batches in Vue

Preface Components are something we use very ofte...

The difference between redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on a ...

How to use Docker+DockerCompose to encapsulate web applications

Table of contents Technology Stack Backend build ...

How to enhance Linux and Unix server security

Network security is a very important topic, and t...

Nodejs implements intranet penetration service

Table of contents 1. Proxy in LAN 2. Intranet pen...