Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize function rewriting by JavaScript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces JavaScript how to achieve function rewriting, the article introduces in great detail, has a certain reference value, interested friends must read it!

Function rewriting

Because one function can return another function, you can overwrite the old function with the new function.

Function a () {console.log ('a'); a = function () {console.log ('b');}}

In this way, when we call the function for the first time, console.log ('a') will be executed; the global variable a will be redefined and assigned a new function

When the function is called again, console.log ('b') is executed.

A more complicated situation is as follows:

Var a = (function () {function someSetup () {var setup = 'done';} function actualWork () {console.log (' work');} someSetup (); return actualWork;}) ()

We use the private functions someSetup () and actualWork (), and when function a () is called for the first time, it calls someSetup () and returns a reference to the function actualWork ().

These are all the contents of the article "how to implement function rewriting in JavaScript". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report