Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【综合】如何理解钩子函数? #21

Open
Dliling opened this issue Jun 9, 2020 · 0 comments
Open

【综合】如何理解钩子函数? #21

Dliling opened this issue Jun 9, 2020 · 0 comments

Comments

@Dliling
Copy link
Owner

Dliling commented Jun 9, 2020

钩子函数源于Windows的消息处理机制,通过设置钩子,应用程序可以对所有消息事件进行拦截,然后执行钩子函数,对消息事件进行想要的处理方式。

JS获取DOM数据的两种方式:

  1. JS派函数监听事件,监听到事件之后执行的函数,就是钩子函数
var btn = document.getElementsByTagName('button');
btn.addEventListener('click', function() {
    // doSomething
});
  1. JS预留函数给DOM事件,DOM事件调用预留的函数,这个预留函数就是回调函数
btn.onclick =  function() {
    // doSomething
}

二者的区别:

  1. 钩子函数是被动地被监听着,一旦满足条件就执行,被监听到多次的时候会被执行多次。
    回调函数是主动触发函数,然后执行函数,一般是局部的。
  2. 钩子函数在捕获消息的第一时间就会执行,有优先控制权。
    回调函数是在整个过程结束后才会执行。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant