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

What are the ways to use the JavaScript function

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the use of the JavaScript function?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The first way to use it: as a separate function

The code is as follows

Function testFunction (A, a) {return A * a;} testFunction (10, 2); / / testFunction (10, 2) returns 200.

In this example, the function test is executed in the call, in which case the function is a global object in JavaScript. Because the default global object in html is the page itself, the function also belongs to the html page. If the page object is a browser window in the browser, the function test automatically becomes a function of the windows object, in which case testfunction () and window.testFunction () are no different.

The second way to use it: functions are used as global objects

The code is as follows

Function mytestFunction () {return this;} mytestFunction ()

In this case, the function does not have its own object call, the value of this becomes the global object, and the value of this is the windows object, if the global object is the browser window in the browser.

The third method of use: functions are used as methods

You can also define object methods (functions) in JavaScript. The code is as follows:

Var myshili = {xing: "zhang", ming: "san", mytest:function () {[xss_clean] (this.xing + "" + this.ming)}} myshili.mytest ()

The above creates an object myshili, finds an object with two properties xing and ming, and a method mytest, which returns a value of "zhang san".

The fourth method of use: the constructor calls the function

The keyword new appears before the function call, and the constructor is used, which is not simply creating a new function, but an object recreated by the javascript function. The code is as follows:

Function testFunction (arg1, arg2) {this.firstName = arg1; this.lastName = arg2;} var x = new testFunction ("John", "Doe"); x.firstName

The new object inherits the properties and methods of the constructor.

This is the end of the content of "how to use the JavaScript function". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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