_javascript techniques for function details in JavaScript

Source: Internet
Author: User

The keyword function is used to define functions.

Copy Code code as follows:

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

Note that the curly braces in a function statement are required, even if the body of the functions contains only one statement.

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

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

Object attributes of a function

Because a function is an instance of a functions, the function name is only a reference address for that instance. It can therefore be used as a parameter and a return value to participate in the function's reference process.

Copy Code code as follows:

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 functions

Arguments | This
The arguments object holds the arguments passed to the function
Arguments.length Returns the number of incoming arguments
The Note:length property represents the number of parameters that are received by default when the function is defined. Arguments.length represents the number of parameters that the function receives when it actually executes.

Copy Code code as follows:

function test_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 () is primarily used in situations where the function itself is invoked in a recursive function. JS and other languages differ in that the function name is just a pointer, can be changed at any time, the function of the function name to call itself belong to high coupling, there may be problems, and Arguments.callee () call itself will circumvent this problem

Copy Code code as follows:

function factorial (num) {
if (num <= 1) {
return 1;
} else {
return num * factorial (num-1);
};
}
function Callee_f (num) {
if (num <= 1) {
return 1;
} else {
Return num * Arguments.callee (NUM-1);
};
}
Factorial (10); Running normally
f = factorial;
factorial = null;
f (10); Error
Callee_f (10); Running normally
f = callee_f;
Callee_f = null;
f (10); Running normally

This is primarily used to help functions refer to objects in the scope of a function.

Copy Code code as follows:

var color = ' red ';
function Syacolor () {
Console.log (This.color);
}
Syacolor (); Red
var 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. As mentioned earlier, the function is a defined object, so when you call the function, this is the call to the current and the next variable. If you want to change the domain space in which the function is performed, you can use call () and apply () to implement it.

Copy Code code as follows:

color = ' red ';
var o = {color: ' Blue '};
function Saycolor () {
Console.log (This.color);
}
Saycolor (); Red
Saycolor.call (this); Red
Saycolor.call (o); Blue

The role of app () and call () is the same, and the difference is mainly in the difference between incoming parameters.

Call (THIS,PARA1,PRAR2,PRAR3) The first argument is the scope in which the function is to be executed, followed by the input parameters of the function, and how many to write sequentially.

Apply (THIS,[PARA1,PARA2,PRARA3]) The first argument is also the scope of the function to perform, followed by an array object for an array.

The biggest advantage of using call ()/apply () to augment the scope is the decoupling of objects and methods.

Built-in objects

The global object can be understood as the outermost object, all objects, and properties and methods that are not part of other objects are included in the global object.
* isNaN (x) is used to check whether the parameter x is a number. Returns true if the number is returned false
* Isfinite (x) is used to check whether the parameter x is infinity/small, and returns true if it is infinitely large/small.
* parseint (x) to parse a string and return an integer
* PARSEFLOAT (x) to parse strings and return floating-point numbers
* encodeURI () and encodeURIComponent () make a special UTF-8 encoding of strings, avoiding some special characters for the browser to read. The main difference between the two is that encodeURI () does not encode special characters that are part of the URI, and encodeURIComponent () encodes all non-standard characters it discovers.

Copy Code code as follows:

var uri = "Http://www.wrox.com/illegal Value.htm#start";
Http://www.wrox.com/illegal%20value.htm#start
Console.log (encodeURI (URI))
Http%3a%2f%2fwww.wrox.com%2fillegal%20value.htm%23start
Console.log (encodeURIComponent (URI))

• The corresponding decoding function is decodeURI () and decodeURIComponent ()
eval (script) is 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 many additional tasks and features.

The Math object is another built-in object. Provides a mathematical computing function for JavaScript.

The above is the entire content of this article, I hope that the small partners can enjoy, can be helpful to everyone.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.