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 use public and private methods in javascript

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

Share

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

This article mainly introduces "how to use public methods and private methods in javascript". In daily operation, I believe that many people have doubts about how to use public methods and private methods in javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use public methods and private methods in javascript". Next, please follow the editor to study!

In javascript, public methods are methods that can be accessed and called externally, while private methods are methods that are declared in the object's constructor that are not visible and accessible externally.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

One: public method

A public method is a method that can be accessed and called externally.

/ / var test1 = {name:' name; this.age', getName:function () {console.log (this.name);}} / / call test1.getName (); / / name; this.age = age; / / Public method this.getName = function () {console.log (this.name) in the constructor }} / / in the prototype test2.prototype.getAge = function () {console.log (this.age);} / / call var test3 = new test2 ('rookie', 12); test3.getName (); / / rookie test3.getAge (); / / 12

Two: private methods and public methods

A privileged method is a public method that has access to internal private properties and private methods (a method that can access private properties is called a privileged method, which is also a kind of public method).

A private method is a method that is declared in the constructor of the object and is not externally visible and inaccessible.

Private and privileged methods are defined in different ways in different forms

In the object, we use the Object object expression to create an object and add some properties and methods, and then call it statically. Such as Rest.getName ()

The private data of the immediate execution function object is placed in an anonymous function execution expression (IIFE), which means that the function exists only at the moment of being called and is destroyed as soon as it is executed.

Var yourObject = (function () {/ / Private properties and methods return {/ / Public methods and properties}) ()

This is the same as the previous definition of Rest, which can be accessed directly through yourObject. Such modular access is quite powerful.

Var test4 = (function () {/ / private attribute var total = 10; / private method var buy = function () {total--;} var get = function () {return total;} return {name:' white', getTotal:get,// uses closures to introduce the use of internal private variables buyfood:buy}}) () Test4.buyfood (); console.log (test4.name); / / Xiaobai console.log (test4.getTotal ()); / / 9

Closures are used to indirectly use internal private variables

It is convenient to define private properties and methods in the constructor. Instead of using closures, we can initialize the data when called.

/ / function test5 (name) {/ / private attribute var total = 10 in the constructor; / / public attribute this.name = name; / / private method function _ buyFood () {total--;} / / privileged method to access private property and private method this.buy = function () {_ buyFood ();} this.getTotal = function () {return total }} / / Public method. Note that private member _ totaltest5.prototype.getName = function () {/ / console.log (_ total); / / Uncaught ReferenceError: _ total is not defined return this.name;} var test6 = new test5 ('size white'); console.log (test6.getName ()); / / 'size white' test6.buy (); console.log (test6.getTotal ()); / / 9

Combined use

Some initialized data can be passed in using the constructor, but private member properties cannot be accessed in public methods. If there are many public methods that need to access private data, we will all write them with privileged methods. In the end, we will bring a lot of unnecessary methods to each instance.

Var test7 = (function () {/ / Private attribute var total = 10; / Private method function buyFood () {total--;} / / Constructor function test7 (name) {this.name = name; this.getTotal = function () {return total) }} / / Public method here is not private test7.prototype.buy within test7 = function () {console.log (total); buyFood ();} test7.prototype.getName = function () {return this.name;} return test7;}) (); var test0 = new test7 ('big white'); console.log (test0.getName ()); / / big white test0.buy () / / 10console.log (test0.getTotal ()); / / 9 at this point, the study on "how to use public and private methods in javascript" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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