简体   繁体   中英

Event function declaration

Im trying to understand the declaration of the event handler and the function declaration in this post: https://stackoverflow.com/a/46418688/7727532

Im pretty new to Javascript and jQuery and trying to understand. What I do know is that you declare like the following

$('#clickMe').on('click', doSomeThing(e));

function doSomeThing(e){
 console.log(e);
}

The first part I dont understand is

(e) => this._handleScaling(e)

What is happening here? We catch the current object and sending it to function _handleScaling()? But why do we have "(e) =>" before?

Second part is

_handleScaling(e) {

Which if Im right is a local function inside another? Because of the underline start. So maybe the code in the link isnt telling the whole picture?

You need to give function reference rather than function's return value

Replace

$('#clickMe').on('click', doSomeThing(e));

with

$('#clickMe').on('click', doSomeThing); //e is the event object will be automatically passed on to the event handler

The first part I dont understand is

(e) => this._handleScaling(e) What is happening here? We catch the current object and sending it to function _handleScaling() ? But why do we have "(e) =>" before?

No, _handleScaling is only passed e (event object) so that it can get access to element on which this event has been triggered using e.target since with arrow function this will refer to scope in which this arrow function is defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM