js--function

Source: Internet
Author: User

I. Types of functions 1. Parameterless function ShowName () {alert ("I am a parameterless function");} 2. The parameter in the function of the argument is the formal parameter form parameter: The parameter specified when defining the function, the actual parameter is determined by the actual parameters: the parameters specified when the function is called, the value of the argument affects the formal parameter */function showname (name) {alert ( name);} Second, the function calls the way 1. Directly call function ShowName (name) {alert (name);} ShowName (); 2: Function calls are used in conjunction with element events, call format (and element event binding) Event name = "function name ()" onclick= "ShowName (' A ')", anonymous function anonymous function is received by variable. Variable name + () allows the function to execute var show = function () {alert ("1111");}; Show ();  the self-invocation method of the anonymous function (function () {alert ("I am an anonymous function!") ")}); Method two (function () {alert (" I am an anonymous function! ")} (); method three!function () {alert (" I am an anonymous function! ")} (); The return keyword returns the computed result of a tuned function to the main key function. Function Calc (num1,c,num2) {switch (c) {case ' + ': var result = Num1 + num2; break;case '-': var result = num1-num2; Case ' * ': var result = Num1 * NUM2; Break;case '/': var result = num1/num2; return Result;/*alert (Result), */}var r = Calc (parseint ("Enter first Number:"), Prompt ("Enter arithmetic character:"), parseint (Prompt (" Please enter the second number: ")); alert (R); v. Uncertainty of arguments 1, in the case of an indeterminate number of arguments, you can omit the formal parameter, and use argumentsargument inside the function body is an array containing all the arguments of the function call! Function Show () {alert (arguments[0]);} Show ("AAA "); 2, the Arguments.callee property Arguments.callee property points to the function itself, which can be used for recursive function show () {Console.log (Arguments.callee);} Show ("AaB"); this property of 3.arguments var Zhangsan ={name: "Zhangsan", Age: "", Height: "189", Say:function () {alert ( ZHANGSAN.NAME);/* F12 View Console Property */console.log (this);},eat:function () {alert ("Hamburger! ");},}zhangsan.say (); VI, Variable scope global variable 1. The variable that is written in front of the function 2. Variables that do not use Var modifier do not use var modifier variables, will be a layer to look up, if you find a variable with the same name, the assignment, or overwrite the original value if the variable with the same name is not found, declare a global variable with the same name. A local variable defines a variable inside a function. code example: var num = 10;function Calc1 () {num = 5;alert (num + 15);} function Calc2 () {alert (num + 20);} Calc1 (); CALC2 (); 

js--function

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.