JS -- three roles of functions

  1. Ordinary function
    • Shape parameter 、 Actual parameters 、arguments、return、 Arrow function
    • Private scope ( Stack memory 、 Execution context )
    • Parameter assignment & Variable Promotion
    • Scope chain
    • Stack memory release and non release ( Closure )
    • ...
  2. Constructors ( class )
    • Classes and instances
    • prototype and __proto__
    • instanceof
    • constructor
    • hanOwnProperty
    • ...
  3. Common object

Be careful

  • Every function ( Ordinary function 、 class 、 Built-in class 、Function) All are Function An instance of this class
Object.__proto__ === Function.prototype
Function.__proto__ === Function.prototype
 Copy code 
  • All the objects are Object An instance of this class
  • Functions are also objects ( It has properties :prototype、__proto__、length、name ...), So all the functions are also Object An instance of this class
function Foo() {
getName = function () {
console.log(1)
}
return this
}
Foo.getName = function () {
console.log(2)
}
Foo.prototype.getName = function () {
console.log(3)
}
var getName = function () {
console.log(4)
}
function getName() {
console.log(5)
}
Foo.getName() //2
getName() //4
Foo().getName() //1
getName() //1
new Foo.getName() //2 new (Foo.getName)()
new Foo().getName() //3 (new Foo()).getName()
new new Foo().getName() // 3 new ((new Foo()).getName)()
 Copy code 
 About priorities A = B > C
A: Foo.getName
B: new Fn()
C: new Fn
therefore :
new Foo.getName(): The first step is to give priority to let f = Foo.getName, The second step new f() => new (Foo.getName)().
new Foo().getName(): Equal priority , From left to right => (new Foo()).getName().
new new Foo().getName() // The first step is to create an instance let f = new Foo(), become new f.getName(), f.getName Priority is greater than new f, So first execute let f2 = f.getName , Finally, execute new f2() => new ((new Foo()).getName)()
 Copy code 
Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15