1024programmer Java Brother, please lend me the lawnmower and I’ll mow the lawn. Let’s talk about the indissoluble bond between this and JavaScript function calls…

Brother, please lend me the lawnmower and I’ll mow the lawn. Let’s talk about the indissoluble bond between this and JavaScript function calls…

When writing the last blog post about apply and call (chat about apply and call in JS) ,At first I was worried that it would be difficult for everyone to understand,because the prerequisite for understanding the apply calling method is ,At least first understand what a function call is in Javascript, what does this mean, etc. However, judging from everyone’s feedback, “my worries are unnecessary” and all gardeners are masters. Understanding these basic things is a piece of cake.
Having said that, today I will still talk to you about the various function calling methods related to this in Javascript. This can complete the knowledge and make it easier to review in the future. .

[Background introduction] Guangming Community is a villa community – every household has a small lawn in front of and behind the house. In order to improve the satisfaction of the owners, the property management company of the community has provided lawn mowing services for the owners of the community, and has also included the gasoline needed for the lawn mowers in the monthly budget to ensure that it can be used at any time. All can provide high-quality services.

Lawn mowing provided  Service lawnmower

Next, we will explain the function calling method in Javascript based on specific business scenarios.

1. Method call

In view of the situation of providing lawn mowing services to owners in Guangming Community,We can use the following code to describe:

var GuangMing = {oilVolume:5000,cutGrass:function( grass_num ){var oilConsumption = grass_num * 20 ; //Assume that every square meter of lawn requires 20 ml of oilconsole.log( & #39;Cutting ' #39; mL.' );console.log( 'It turns out there is oil:' + this. oilVolume + ' mL' );this.oilVolume = this.oilVolume – oilConsumption ;console.log( 'After using up, the remaining oil volume is:' + this.oilVolume &# 43; ' mL' );return ;}
};
GuangMing.cutGrass(
15 ) ; //There is 15 square meters of lawn at the entrance of the community that needs to be cut
console.log( '>>The remaining oil in Guangming Community:& #39; + GuangMing.oilVolume );

After running in the browser console, the output result is :

15 square meters are being cut. Lawn requires 300 mL of oil.
It turned out that there was oil:
5000 mL
After using up, the remaining amount of oil was:
4700 mL
>>The remaining oil in Guangming Community:4700

This is the so-called method call, and it is the same as we expected (especially after working with static languages ​​​​such as Java before The master),has no sense of disobedience.
“oilVolume and cutGrass are both members of the GuangMing object. The this.oilVolume called in the cutGrass method certainly refers to the oilVolume that belongs to the same object (GuangMing).”
However, ,our above understanding is essentially,wrong.
This.oilVolume referenced in the cutGrass function body refers to the variable GuangMing.oilVolume – not because both cutGrass and oilVolume are both “declared” in the GuangMing object The members of the GuangMing object are ‘but because’ we use the cutGrass function through GuangMing.cutGrass(15);
.
In other words:Who this in the function body points to does not depend on the declaration time, but depends on the runtime.
We will gradually improve the complete conclusion with the following explanations.

2. Function call

After the launch of this service in Guangming Community, it has been well received by the majority of property owners. The Hongqi Subdistrict Office to which the community belongs also knew about this. The comrades in the Subdistrict Office thought that this service was a good measure to enhance the image of the unit, so they also announced that they could provide lawn mowing services. However, the Hongqi Subdistrict Office did not purchase the lawnmower equipment itself. Instead, when someone requested service, it simply notified the property management company of Guangming Community to help provide it.

The code is expressed as follows:

1 var oilVolume = 8000 ;
2
3 var GuangMing = {
4 oilVolume:5000,
5 cutGrass:function( grass_num ){
6 var oilConsumption = grass_num * 20 ; //Assume that every square meter of lawn requires 20 ml of oil span>
7 console.log( 'Cutting'”
Lao Zhang:”Okay,You register here,This is the key to the lawnmower,Let Uncle Wang go with you. “
Xiaodong found Lao Wang , and asked him to help him go to the “Satellite Farm” to mow the grass. Lao Wang asked about the situation , told Xiaodong, #xff1a;”Where is the lawn in the satellite farm? At that time, there was a wheat field.”.

The same scene, happened in the Javascript community Different situations will happen,
Xiaodong:”Uncle Zhang,Today Xiaojie and I want to go to the “Satellite Farm” to cut the grass, Can you help me?”
Lao Zhang:”Okay,This is the key to the lawnmower,You can take it and use it. “
When he arrived at the 'Satellite Farm', Xiaodong started the “lawn mower” and in a short time he cut down the wheat in a wheat field.
My diary after I came back said this: “Today’s weather is good and the sky is cloudless with white clouds… This is really meaningful.” What a day!”
[Analysis]
In static languages ​​(such as Java), the parameter type and number of parameters of the method will be checked during the compilation phase. ,If something is wrong, it will prompt “Compilation error” The type and quantity are checked.
In dynamic languages ​​(such as Javascript), methods will not be checked in this regard. If the method is compared to Lawnmower',So for the Javascript engine, it doesn’t care whether you are mowing “grass” or “wheat” xff0c;Whether you add ‘gasoline’ or ‘soy sauce’ to the fuel tank of the ‘lawn mower’ when it is running' #xff0c; I will point out the error to you only after I find the error.
Although Javascript does not provide type checking services at the function level, as a supplement, it also provides typeof, instanceof and other type checking services. The way,So,we often see that the beginning part of the function body of some functions,is a bunch of if-else,checking the incoming parameters.
Remembered The guards and security guards at the college entrance examination room. Did you bring your admission ticket? Those who didn’t brought it went out. Did you bring your mobile phone? Those who did bring it also went out. Take off your clothes….’ ;
There is no way,After all,'candidates'are not'security guards','security guards'are not' Candidate'.
“Xiao Li,This is the son of Boss Du. “
“…”

2. Method invocation

In static language (Java), when we need to use an object (class) When it comes to methods, we must at least call them through this object (class), or inherit this class, and then use the method of the parent class in the subclass, or add this class to the method. The object is used as a member of the current object and is then called through this member. It is equivalent to using the community’s lawnmower. At least it should say hello to register. .
But in dynamic languages ​​(Javascript), functions seem to exist independently in memory (we know that in Javascript, functions are also objects), Although it is declared as if this function “belongs” to object A, it is only when it is actually used (runtime) that it becomes clear who it “belongs to” #xff0c;Who does this in the function body refer to?
And ,there are not many restrictions on referencing a function,just like you have the key to the lawnmower,you You can start this lawn mower , use this lawn mower.

Obviously,certain features of Javascript (prototype chain, apply calling method, high-order function attributes, Closures, etc.) make Javascript very flexible and of course prone to errors. It’s the same as real life – there are too many rules and regulations and you will feel that you are not free and your behavior is restricted. #xff0c;But ,without some rules and constraints,without parents’ nagging and reminders,sometimes mistakes will indeed be made.
So,when developing applications using Javascript On the one hand, we use programming standards (common agreements) to reduce the occurrence of errors. For example, “in the community” everyone agrees to borrow from the “owner’s home” The ladder must be returned after use. On the other hand, we can also use certain features in Javascript (such as closures) to build better encapsulation and stability. Good and flexible applications. For example, we were worried that kids using the community lawnmower might mistake soy sauce for gasoline. Pour it into the fuel tank of the lawn mower. At this time, we can lock the fuel tank of the lawn mower and prevent you from adding anything into it.
Obviously naughty kids are not good at Javascript. More often than not, we have to use Javascript because the applications we want to develop can only be developed using it. Since there is no other choice, there is only one way to “study hard”. As a senior said, “Only if we understand its essence” and “its dross” can we truly make good use of it.
It seems like someone is knocking on the door, It should be that the midnight snack ordered online has been delivered…
Okay,That’s all for today’s chat with everyone,Thank you for your support .

Redirect: https://www.cnblogs.com/alai88/p/5525615.html

On the way. As a senior said, “Only if we understand its essence” and “its dross” can we truly make good use of it.
It seems like someone is knocking on the door, It should be that the midnight snack ordered online has been delivered…
Okay,That’s all for today’s chat with everyone,Thank you for your support .

Redirect: https://www.cnblogs.com/alai88/p/5525615.html

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/brother-please-lend-me-the-lawnmower-and-ill-mow-the-lawn-lets-talk-about-the-indissoluble-bond-between-this-and-javascript-function-calls/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索