Javascript-what is a "hook "?

Source: Internet
Author: User
Please explain what is "hook ". Please explain what is "hook ".

Reply content:

Please explain what is "hook ".

Hook. It is translated as a Hook;
For example:
You have a wire bound on your key and a hook on it online. You can put the key on a hanger or on your curtain. Well, this may not be vivid enough. Let's try again:

Here, this hanger is equivalent to a hook. The Hook can be used to hook the towel to it.

This is probably the meaning. in the program, you define a small function that can be mounted wherever you want to use it. This small function uses hooks, such as the comment list display. It can be a hook and can be mounted as needed.
This is similar to the plug-in, but I think the plug-in features are more complex than hooks, and the hook features are simpler and more flexible.

Therefore, I think the things that conform to the hook idea are hooks, so are programs and things in life.

I don't know whether it is accurate. A specific statement is called a callback function. There are many such mechanisms in the Linux kernel. The function pointer in C language is used at the underlying layer to call the function. The top layer points the pointer to a function to decouple the code.

"Hook" is an image name. In a place where a hook can be placed, we call it a "mount point"

My personal understanding of hooks is to insert custom content in a special position (mount point) in an ordered step. This is called "Hook ".

In ThinkPHP, there are front hooks and rear hooks used to set custom operations before and after adding, deleting, modifying, and querying data.

In CodeIgniter, hooks are more flexible, flexible, and early hooks are executed. It can satisfy the developers' geeks.

The hooks I understand refer to a way to isolate the easily changed parts in the parent Algorithm Framework of the template method mode.
For example, in the algorithm framework init () of the parent class, operations A-operations B-Operations C are executed by default. Normally, most of its child classes follow this process, however, if a specific subclass does not want to perform B operations, we can encapsulate a hook invokeB in the parent class. The default value is true, execute the B operation only when it is true, and then set the inherited invokeB to false in the subclass, so that the init () of the subclass is called () the B operation will be skipped.

I am also coming to join in. I think hook is:

Provides an opportunity to influence the default (or original) process (mechanism ).

Generally, a database, a framework, a system, or a language provides an open interface through which users can influence the behavior of libraries, frameworks, systems, or programs.

As for the specific implementation, it is very different. For example, some people mentioned the template method in Java.

The most intuitive understanding is that you want to access a post. However, if this post has permission verification, you must log on to the member to access the post. The process of permission verification can also be called a hook. Simply put, it is triggered by a certain condition. is a hook.

Use javascript to give an example:
Alert is a js native function, right? We want to transform the alert function. At this time, oriAlert is a hook function.

Var oriAlert = window. alert; window. alert = function (txt) {// transform the function oriAlert (txt );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.