Home >Web Front-end >JS Tutorial >Introduction to the three roles of functions in js
Note: Function.prototype is the value of the function data type, but the related operations are exactly the same as before->Empty/anonymous
The function itself will also have some properties of its own:
length: the number of formal parameters
name :"Fn" function name
prototype The prototype of the class, the methods defined on the prototype are all public methods of the current Fn class instance
__proto__ treats the function as an ordinary object, pointing to the prototype of the Function class
Function is the most complex and important knowledge in the entire JS:
1. A function has multiple aspects:
"Ordinary function": It is an ordinary function in itself. During execution, a private scope (closure) will be formed, formal parameter assignment, pre-interpretation, code execution, and the stack memory will be destroyed or not destroyed after execution is completed
"Class ": It has its own instance, and also has an attribute called prototype which is its own prototype. Its instances can all point to its own prototype
"Ordinary Object": Like obj in var obj = {}, it is an ordinary object. As an object, it can have its own private attributes, and Function.prototype can also be found through __proto__
There is no necessary relationship between these three.
function Fn(){var num = 500;this.x = 100; } Fn.prototype.getX = function(){ console.log(this.x) } Fn.aaa = 1000;var f = new Fn; f.num //undefinedf.aaa//undefinedvar res = Fn(); res//undefinedFn.aaa//1000
The above is the detailed content of Introduction to the three roles of functions in js. For more information, please follow other related articles on the PHP Chinese website!