JavaScript---Function

Source: Internet
Author: User

The keyword function is used to define a function.

function declaration Definition:
function FuncName ([ARG1[,ARGS[...,ARGN]]) {
Statements
}
function Expression Definition:
var funcname = function ([arg1[,args[...,argn]]) {
Statements
};

Note that the registration of a flower in a function statement is required, even if the body contains only one statement.

In JavaScript, a function is a specific instance of the function class. And they all have properties and methods like other reference types.

The function name is actually a pointer to the function object, and the function can participate as a parameter in the arguments and return values.

Object properties of a function

Because a function is an instance of a function, the name of the functions is simply a reference address for that instance. You can therefore participate in the function's argument as a parameter and as a return value.

function call_some_function(some_function, some_argument) {    return some_function(some_argument);}function add_10(num) { return num + 10;}console.log(call_some_function(add_10,20)); //30
Internal properties of the function

arguments|this

    • argumentsObject that holds the arguments passed to the function
    • arguments.lengthReturns the number of parameters passed in
    • Note: length The property represents the number of parameters that are received by default when the function is defined. arguments.lengthrepresents the number of arguments received when the function actually executes.
functiontest_arguments () {if (arguments.length = = 2" {console.log (arguments.length); console.log (arguments);} else {console.log ( Arguments.length); console.log (arguments); arguments.callee (4, 5);};} (1, 2, 3) /** 3{' 0 ': 1, ' 1 ': 2, ' 2 ': 3}2{' 0 ': 4, ' 1 ': 5} **/     
    • arguments.callee()It is primarily used in situations where the function itself is called in a recursive function. The difference between JS and other languages is that the function name is just a pointer, can change at any time, the function of using the function name to call itself is high coupling, there may be problems, and arguments.callee() the call itself will avoid the problem
functionFactorial(num) {if (Num <=1) {Return1; }else {return num * factorial (num-1); };}function callee_fif (num <= 1) { return 1;} else {return num *  Arguments.callee (Num-1);};} Factorial (10); //run normally f = factorial;factorial = null;f ( 10); //errorcallee_f (10); //run normally f = Callee_f;callee_f = null;f ( 10); //run normally             
    • thisUsed primarily to help a function refer to an object in the scope of the function.
var color = ‘red‘;function syaColor() { console.log(this.color);}syaColor(); //redvar o = new Object();o.color = ‘blue‘;o.sayColor = sayColor;o.sayColor(); //blue
Call () and apply ()

Call () and apply () are the own methods that each function contains. It has been mentioned before that a function is a defined object, so when calling a function, this it is a call to the current and next variable. If you want to change the domain space where the function executes, you can use call () and apply ().

‘red‘;var o = {color: ‘blue‘};function sayColor() { console.log(this.color);}sayColor(); //redsayColor.call(this); //redsayColor.call(o); //blue

Apps () and call () are the same, and the difference is mainly in the parameters that are passed in.

call(this,para1,prar2,prar3)The first parameter is the scope of the function to be executed, and the following arguments are the input parameters of the function, and how many are written sequentially.

apply(this,[para1,para2,prara3])The first argument is also the scope to be performed by the function, followed by an array object of an array.

The call() apply() greatest benefit of using/to extend the scope is the decoupling of objects and methods.

Built-in objects

Globalobjects can be interpreted as outermost objects, and all objects, as well as properties and methods that are not part of other objects, are included in the global object.
* isNaN(x) used to check if the parameter x is a number. Returns True if False is returned for the number
* isFinite(x) used to check if the parameter x is infinity/small and returns True if it is infinity/small
* parseInt(x) used to parse a string and return an integer
* parseFloat(x) used to parse strings and return floating-point numbers
* encodeURI() and the encodeURIComponent() string will be special UTF-8 encoding, avoid some special characters so that the browser can read. The difference between the two is that the encodeURI() special characters that belong to the URI are not encoded, and encodeURIComponent() all non-standard characters that they discover are encoded.

"http://www.wrox.com/illegal value.htm#start";//http://www.wrox.com/illegal%20value.htm#startconsole.log(encodeURI(uri))//http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23startconsole.log(encodeURIComponent(uri))
    • The corresponding decoding function is the decodeURI() anddecodeURIComponent()
    • eval(script)Used to execute the contents of the script in the interpreter and return the corresponding result. Very powerful!

Note: In the browser, the Windows object encapsulates the global object and takes on a lot of additional tasks and functionality.

MathObject is another built-in object. Provides mathematical computing capabilities for JavaScript.

JavaScript---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.