The keyword function is used to define a function.
The code copy is as follows:
// Function declarative definition:
function funcname([arg1[,args[...,argn]]]){
statements
}
// Function expression definition:
var funcname = function ([arg1[,args[...,argn]]]){
statements
};
Note that curly braces in function statements are required, even if the function body contains only one statement.
In JavaScript, functions are specific instances of 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 be used as a parameter to participate in the parameter transfer and return value.
Object properties of functions
Because a function is an instance of a Function, the function name is only a reference address of that instance. Therefore, it can be used as parameters and return values in the function's parameter transfer process.
The code copy is 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 a function
arguments | this
•The arguments object holds parameters passed to the function
•arguments.length returns the number of incoming parameters
•Note: The length attribute represents the number of parameters received by default when the function is defined. arguments.length represents the number of parameters received when the function is actually executed.
The code copy is 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 mainly used in the situation where the function itself is called in recursive functions. The difference between js and other languages is that the function name is just a pointer and can be changed at any time. The function name in the function is highly coupled, which may cause problems, and the call to arguments.callee() itself will avoid this problem.
The code copy is 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); //Operate normal
f = factorial;
factorial = null;
f(10); //error
callee_f(10); //Operate normally
f = callee_f;
callee_f = null;
f(10); //Operate normal
•This is mainly used to help functions refer to objects in the scope of the function.
The code copy is 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 own methods that each function contains. It has been mentioned before that functions are defined objects, so when calling functions, this in the function is a call to the current and lower variables. If you want to change the domain space where the function is executed, you can use call() and apply() to implement it.
The code copy is as follows:
color = 'red';
var o = {color: 'blue'};
function saysColor() {
console.log(this.color);
}
sayColor(); //red
sayColor.call(this); //red
sayColor.call(o); //blue
The functions of app() and call() are the same, and the difference is mainly the difference in the incoming parameters.
call(this,para1,prar2,prar3) The first parameter is the scope of the function to be executed. The subsequent parameter is the input parameter of the function, and there are so many times written in sequence.
apply(this,[para1,para2,prara3]) The first parameter is also the scope of the function to be executed, followed by an Array array object.
The biggest benefit of using call()/apply() to expand scope is the decoupling of objects and methods.
Built-in objects
Global objects can be understood as the outermost object, all objects, as well as attributes and methods that do not belong to other objects are included in the Global object.
* isNaN(x) is used to check whether the parameter x is a number. If false is returned for a number, return true otherwise
* isFinite(x) is used to check whether the parameter x is infinite/small. If it is infinite/small, it returns true.
* parseInt(x) is used to parse strings and return integers
* parseFloat(x) is used to parse strings and return floating point numbers
* encodeURI() and encodeURIComponent() will perform special UTF-8 encoding on the string, avoiding some special characters for the browser to understand. The main difference between them is that encodeURI() does not encode special characters that belong to the URI, while encodeURIComponent() encodes all non-standard characters it finds.
The code copy is 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 functions are decodeURI() and decodeURIComponent()
•eval(script) is used to execute script content in the interpreter and return the corresponding result. Very powerful!
Note: In the browser, Windows objects encapsulate Global objects and undertake a lot of additional tasks and functions.
The Math object is another built-in object. Provides mathematical calculation functions for JavaScript.
The above is all about this article. I hope you like it and it will be helpful to you.