Preface
existJavaScript
In this case, the scope of the function is a very important concept. According to the default behavior, a function can only access its own scope and variables declared in its parent scope.
However,JavaScript
There are some methods to change the scope of a function. In this blog, we will introduce these methods and compare the pros and cons between them.
Use call and apply methods
call
andapply
yesJavaScript
Both methods in it can be used to change the scope of the function.
call
Methods allow you to call a function, and when called, you can specify the value of this inside the function, as well as the parameters associated with the function. For example:
function myFunction(a, b) { (this); (a + b); } ({ name: 'John' }, 2, 3);
In the above example,myFunction
The function is called, passing an object tocall
method. This object is set to the function insidethis
value. In the call, 2 and 3 are also passed to the function and are added together.
apply
Methods andcall
Similar, but it receives aParameter array, not a comma-separated parameter list.
({ name: 'John' }, [2, 3]);
call
andapply
The advantage of the methods is that they areEasy to use and understandof.
They can conveniently solve some simple problems, such as using the same function in different contexts.
shortcomingIt is that they cannot be used in more complex scenarios, such as calling a function in the prototype context of a certain function.
Use bind method
bind
Methods can also be used to change the scope of a function, but they behave slightly differently.
``bindThe method returns a new function where
thisThe value is set to pass to
The object of the bind` method, and this new function alsoNot executed. Any arguments passed to the bind method will be used as arguments to the new function. For example:
var boundFunction = ({ name: 'John' }, 2, 3); boundFunction();
In the above example,myFunction
The function is bound to a new function. This new function is set to{name:'John'}
. When a new function is called, 2 and 3 are also passed to it.
bind
MethodadvantageIt's very flexible because it can create a new function so that we can use it in many cases.
However,shortcomingIt requires additional memory to create a new function object.
Use arrow functions
ES6
Introduced an arrow function that can use the current context'sthis
Values, and unlike regular functions, no one belongs to their ownthis
value.
For example:
var myObject = { name: 'John', myFunction: function(){ setTimeout(() => { (); }, 1000); } }; (); //Output "John"
In the above example, the arrow functionthis
The value is set to the external functionthis
Context, i.e.myObject
。
Arrow functionadvantageIt's them that eliminatethis
The confusion of binding, while maintainingJavaScript
simplicity.
However, since they do not have their ownthis
values, they cannot change it.
Summarize
In this article, we discuss three ways to changeJavaScript
Function scope:
-
call
andapply
bind
- Arrow function
althoughcall
andapply
Very convenient, but they are not suitable for complex tasks.
bind
The method solves this problem well, but requires extra memory to create new function objects.
Arrow function is a very concise and elegant way to solve itthis
The trouble of binding, but it also has its limitations.
This is the article about JavaScript's method examples to change the scope of function. For more related contents of JavaScript's function change, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!