A the arguments
- Every function except the arrow function has arguments
- Arguments are pseudo-arrays and have no common properties of arrays (push…) Array.prototype.object.prototype
2 this
- Every function except the arrow function has this
- Why use this? Without this, some code would be difficult to write, such as a function that wants to get a reference to an object
// Construct the object
let beauty={
name:'beauty'.run(){
console.log(` see `+beauty.name+'Running'); }}// Use classes to construct objects
class Person{
constructor(name){
this.name=name;// This is mandatory for new
}
run(){
console.log(` see `+xxx.name+'Running'); }}Copy the code
-
We can get the object’s name attribute (reference) using a variable that holds the object’s address, but if beauty is renamed, the run function cannot be used. If you are using a class, you have not created the object yet, so it is not possible to use a reference to the object. How do you get the object’s name attribute?
-
So you need a way to get the object so that you can get its properties. (1) How can you get a reference to an object when you haven’t defined the object and don’t know its name? There is a native method that python uses, which is to write an argument to each function when it is defined. By default, this argument refers to the new object defined later, and when it is called, the new object is passed to it
class Person{
constructor(name){
this.name=name;// This is mandatory for new
}
run(self){
console.log(` see `+self.name+'Running'); }}// So each argument takes an extra self, which refers to the new object defined in the future
let beauty=new Person('beauty');
beauty.run(beauty);
// There are two kinds of beauty
// Writing beauty.run() directly in Python is equivalent to writing beauty.run(beauty);
Copy the code
(2) How does JS solve this problem? Js adds this to each function
let beauty={
name:'beauty'.run(){
console.log(` see `+this.name+'Running'); }}//beauty.run() = beauty.run()
//beauty.run() implicitly passes beauty to run, which can refer to beauty through this
// The js engine implicitly does this=beauty for you
// Each function can implicitly get a reference to an unknown object through this
Copy the code
Three summary
We want the function to get a reference to the object, but not through the variable name. Python does it through the extra self argument, and JS does it through the extra this, which is the object on which the function is ultimately called
Two ways to call four functions
- beauty.run();
It automatically passes beauty to the function as this 2. run. Call (anything); You need to manually pass anything into the function as this
Fn is a normal function and arrow is an arrow function.
-
When new fn() is called, this in fn refers to the newly generated object, which is determined by new
-
In the fn() call, this points to window by default, which is determined by the browser
-
In the obj.fn() call, this points to obj by default, which is js implicitly passing this
-
In the fn.call(XXX) call, this is XXX, which is the developer’s call to display the specified this
-
In arrow(), the this inside the arrow is the this outside the arrow, because the arrow function does not have its own this
-
In arrow.call(XXX), the “this” inside the arrow is still the “this” outside the arrow, because the arrow function does not have its own “this” inside