Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement call function by js

Shulou Source: shulou.com Published: 2022-06-01 03:45:47 09月10日 Update

This article mainly shows you "js how to achieve call function", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "js how to achieve call function" this article.

The implementation steps of the call function:

1. To determine whether the calling object is a function, even if we are defined on the prototype of the function, it may be called using call and so on.

two。 Determines whether the incoming context object exists, and if not, sets it to window.

3. Process the incoming parameters and intercept all the parameters after the first parameter.

4. Use the function as an attribute of the context object.

5. Use the context object to call this method and save the return result.

6. Delete the properties you just added.

7. Returns the result.

/ / the call function implements Function.prototype.myCall = function (context) {/ / determines the calling object if (typeof this! = = "function") {console.error ("type error");} / / gets the parameter let args = [... arguments] .slice (1), result = null; / / determines whether context is passed, and if it is not passed, set it to window context = context | | window / / set the calling function as the object method context.fn = this; / / call the function result = context.fn (.. ARGs); / / delete the attribute delete context.fn; return result;}. This is all the content of the article "how js implements the call function". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Functions objects parameters up and down context content properties articles methods results learning help prototypes situations ways easy to understand more organization steps knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei Shulou Tech Info OPPO Reno Xiaomi