Methods & Functions
the difference
1.function is a more general concept, such as mathematics and programming
2. method is an object-oriented concept, which generally appears in pairs with classes or objects.
relation
1. The attributes of the object can be of any type
2. If the attribute of an object is a function type, it is called the method of this object.
3. So the essence of a method is still a function
Calling of functions
1.fun()
2.obj.fun()
3.fun.call()
Properties and methods of functions
1.name
2.length
3.toString
Scope
Not all variables can be used anywhere
Global variables (cross files)
var n = 1;function fn(){ console.log(n); // 1}Local variables (only accessible inside functions)
function fn1(){ var n = 2;}console.log(n); // Uncaught ReferenceError: number is not definedFunction scope
• Functions can separate a scope
var n = 1;function f(){ var n = 2; console.log(n); // Variable search in the current scope}f(); console.log(n); // Variable search in the global scope•You can access the outside of the function inside the function
var n = 1;var x = function(){ console.log(n);};function f(){ var n = 2; x();}f();• Self-call anonymous functions
!function (){ var n = 1; console.log(n);};~function (){ var n = 1; console.log(n);};(function(){ var n = 1; console.log(n);}());•Closing
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <ul> <li>aaa</li> <li>bb</li> <li>cc</li> <li>ddd</li> <li>eee</li> </ul> <script> var items = document.getElementsByTagName('li'), i = 0; for (i; i < items.length; i++) { items[i].onclick = function(e){ alert(i); }; } </script> </body></html>The above article in-depth understanding of JavaScript functions is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.