Methods & Functions
Difference
1.function is a more general concept, such as math, programming
2.method is an object-oriented concept that typically appears in pairs with classes or objects
Relationship
1. The properties of an object can be any type
2. The object's property, if it is a function type, is called the method of the object
3. So the essence of the method is still the function
Call to function
1.fun ()
2.obj.fun ()
3.fun.call ()
Properties and methods of a function
1.name
2.length
3.toString
Scope
Variables are not available anywhere
Global variables (cross files)
var n = 1;
function fn () {
console.log (n);//1
}
Local Variables (accessible only within functions)
function fn1 () {
var n = 2;
}
Console.log (n); Uncaught Referenceerror:number is not defined
function scopes
• Functions can be independent of a scope
var n = 1;
function f () {
var n = 2;
Console.log (n); In the current scope of the variable search
}
f ();
Console.log (n); To search for variables in global scope
• Functions can be accessed outside the function
var n = 1;
var x = function () {
console.log (n);
};
function f () {
var n = 2;
X ();
}
f ();
• Self-Invoke anonymous function
!function () {
var n = 1;
Console.log (n);
};
~function () {
var n = 1;
Console.log (n);
};
(function () {
var n = 1;
Console.log (n);
} ());
• Closures
This in-depth understanding of the JavaScript function is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.