JavaScript Basics -5 JavaScript functions

Source: Internet
Author: User

First, JavaScript function definition


Functions (methods, procedures)

-Functions (function), sometimes called methods, or processes (Procedure)

-a block of code that is predefined and can be reused, which can contain multiple executable statements

-function is essentially a fully functional object


Functions that define functions (methods, procedures)

-Define a function using the keyword function

-Definition rules and identifiers for function names are always, case sensitive

-The simplest function definition

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/00/wKioL1b05HvwsUhiAAC98SfUbko836.png "title=" Web.png "alt=" Wkiol1b05hvwsuhiaac98sfubko836.png "/>


Defining parameters and return values

-A slightly more complex function may need to declare the execution parameters

-or use the RETURN keyword to declare the return value of the function

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/03/wKiom1b047ngJkxHAAG0wYapA3M162.png "title=" Web.png "alt=" Wkiom1b047ngjkxhaag0wyapa3m162.png "/>


Second, the invocation of JavaScript functions


Parameter passing when calling a function

-If the function definition specifies that parameters need to be passed, the required value must be provided when calling this function.

The following functions are defined:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/00/wKioL1b05PSCtbzMAABjze6aTzM882.png "title=" Web.png "alt=" Wkiol1b05psctbzmaabjze6atzm882.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/04/wKiom1b05IThzPrGAAFK5B6vHNs040.png "title=" Web.png "alt=" Wkiom1b05ithzprgaafk5b6vhns040.png "/>


Get return value

-If the return value is declared in the function definition, you can receive this return value when the function is called.

The following functions are defined:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/00/wKioL1b05n2htlu5AABCWTZl_Ck531.png "title=" Web.png "alt=" Wkiol1b05n2htlu5aabcwtzl_ck531.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/04/wKiom1b05gXRTKvqAADEDMw5rO4774.png "title=" Web.png "alt=" Wkiom1b05gxrtkvqaadedmw5ro4774.png "/>


function called inside function

-You can call other functions in a function body

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/00/wKioL1b05xTwWqUSAADKy5IR5k0666.png "title=" Web.png "alt=" Wkiol1b05xtwwqusaadky5ir5k0666.png "/>

-complex example of calling other functions in the function body

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/04/wKiom1b05vzQptzmAAFIWXglQKc118.png "title=" Web.png "alt=" Wkiom1b05vzqptzmaafiwxglqkc118.png "/>


Iii. Scope of JavaScript variables


Scope overview

-scope is the accessible range of variables and functions, he controls the visibility and life cycle of variables and functions

-In JavaScript, the scope of variables and functions is divided into:

-function scope, accessible only within the current function

-Global scope, once defined, can be accessed anywhere in the code


Variables in the scope of a function

-In the function scope, the German variable (local variable) is accessible only within the current function and cannot be accessed from this range.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/01/wKioL1b07FyQJ9OSAACoZjZRA3Y585.png "title=" Web.png "alt=" Wkiol1b07fyqj9osaacozjzra3y585.png "/>


Variables in a global scope

-Variables (global variables) in the global scope can be accessed anywhere in the code

-variables defined at the outermost layer are global variables

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/01/wKioL1b07TqhK06SAAHBazscjFc355.png "title=" Web.png "alt=" Wkiol1b07tqhk06saahbazscjfc355.png "/>


Declaration in advance

-the JavaScript program reads all VAR-declared variables and function-declared functions to the top of all scopes before it is formally executed

-but the assignment of the variable is kept in the original position and executed with the script interpretation.

such as

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/05/wKiom1b077Sxt5kmAAFBOyOhae0150.png "title=" Web.png "alt=" Wkiom1b077sxt5kmaafboyohae0150.png "/>

-the declaration can be advanced only to the top of the scope in advance

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/05/wKiom1b08DXRNkXRAACwQcUKDRg437.png "title=" Web.png "alt=" Wkiom1b08dxrnkxraacwqcukdrg437.png "/>

is equivalent to:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/01/wKioL1b08PexFaknAADZDBTQtaY905.png "title=" Web.png "alt=" Wkiol1b08pexfaknaadzdbtqtay905.png "/>


-declaration not only advance the variable declaration, but also advance the function declaration

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/01/wKioL1b09DOQcVxRAABLoCbpbWI635.png "title=" Web.png "alt=" Wkiol1b09doqcvxraablocbpbwi635.png "/>

is equivalent to:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/05/wKiom1b087aBkoPkAABMb2V7HpM853.png "title=" Web.png "alt=" Wkiom1b087abkopkaabmb2v7hpm853.png "/>

-Summary: The declaration of Variables and the declaration of functions should be centered at the top of the current scope as much as possible.


Pass by value

-Passing by value means copying the value of an argument variable to the shape of a method parametric

-When you pass a parameter to a method in JavaScript, it is passed by value

-If the raw type data is passed, the value of the parameter variable is modified within the method without affecting the external real parametric

such as

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/02/wKioL1b09i_iL19qAAFOvcrhOJU379.png "title=" Web.png "alt=" Wkiol1b09i_il19qaafovcrhoju379.png "/>


Scope of the function

-Similar to variables, the scope of the function is also divided into "global scope (global function)" and "function scope (local function)"

-the function defined at the outermost layer is the global function

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/05/wKiom1b09svQTu0yAAB_pgoRDss188.png "title=" Web.png "alt=" Wkiom1b09svqtu0yaab_pgordss188.png "/>

-Functions defined inside a function are local functions

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/02/wKioL1b097SDTOw5AAC_DiOasuo406.png "title=" Web.png "alt=" Wkiol1b097sdtow5aac_dioasuo406.png "/>



Third, JavaScript global functions


Overview of global functions

-a set of global functions is pre-defined in ECMAScript for users to use directly

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/02/wKioL1b0-y_SJ99FAAO3XQzkLBQ852.png "title=" Web.png "alt=" Wkiol1b0-y_sj99faao3xqzklbq852.png "/>

encodeURI and decodeURI

-The encodeURI () function is used to encode a Uniform Resource Identifier (URI), returning the encoded URI string

-The decodeURI () function is used to encode a string after encoding with encodeURI ()

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/02/wKioL1b0_Dvx1OD2AAFMbkGEUzk198.png "title=" Web.png "alt=" Wkiol1b0_dvx1od2aafmbkgeuzk198.png "/>


eval function

-The Eval () function is used to execute JavaScript code that appears as a string

-Use this function to enable JS to execute dynamic JavaScript source code

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/06/wKiom1b0_FSDR94NAAED3sjDpYQ738.png "title=" Web.png "alt=" Wkiom1b0_fsdr94naaed3sjdpyq738.png "/>


Summary: This chapter mainly introduces the JavaScript function

This article from the "Technical Exchange" blog, declined reprint!

JavaScript Basics -5 JavaScript functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.