JS Basics (Public method, private method, privileged method) _ Basics

Source: Internet
Author: User

The topics covered in this article, while very basic, are a small gimmick for many people, but are a comprehensive topic in JavaScript basics. This involves the encapsulation of object attributes, prototypes, constructors, closures, and immediate execution of expressions .

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


var restaurant = {
 name: ' McDonald ' in the object,
 ///Public method
 Getname:function () {return
 this.name;
 }
}

function person
(name, age) {
 this.name = name in the constructor;
 This.age = age;
 Public method
 This.getname = function () {return
 this.name
 }

}
Person.prototype.getAge = function () {return
 this.age
} in the prototype

Private Methods and privileged methods
These two methods are generally discussed together because the privileged method we define is the public method that has access to both internal private and private methods, whereas private methods are externally invisible and inaccessible methods.

There are usually two ways to define an object, either by using object instantiation or by an expression, or by using a constructor. Also in different ways the form of defining private methods and privileged methods is not the same.

In the object
Here we create an object and add some properties and methods by using an object expression, and then invoke it directly in a static way. The private data for an object is placed in an anonymous function execution expression (iife) immediately. This means that the function exists only in the instant that it is invoked, and is destroyed immediately upon execution.

The way in which private data is created in an object is called a module pattern in the object's schema, which refers to the pattern of the object being created, and its basic format is as follows:

var Yourobject = (function () {

 //private property and method return

 {
 //Public method and property
 }
}) ();

In module mode, the object literals returned contain only the properties and methods that can be exposed.

var restaurant = (function () {
 //private Property
 var _total = ten;

 Private Method
 var _buyfood = function () {
 _total--;
 };
 var _gettotal = function () {return
 _total;
 }

 return {
 name: ' McDonald ',
 gettotal: _gettotal, buy
 : _buyfood
 }
}) ();

Restaurant.buy ();
Console.log (Restaurant.name); ' McDonald '
console.log (Restaurant.gettotal ());//9

Note that we used the method of closure to indirectly use internal private variables, while the restaurant (restaurant) name (name) was initialized.

In the constructor
When you create a private method in the module pattern described above, the public and privileged methods are not fundamentally different because the concept is defined when you create private data using a constructor.

It is convenient to define private properties and methods in the constructor, and we do not need to use closures to initialize the data at the time of the call.

function Restaurant (name) {
 //private Property
 var _total = ten;

 Public attribute
 this.name = name;

 Private method
 function _buyfood () {
 _total--
 }

 Privileged method
 this.buy = function () {
 _buyfood ();
 }

 This.gettotal = function () {return
 _total;
 }
}

Public method, note that there is no access to private member _total
Restaurant.prototype.getName = function () {
 console.log (_total);//uncaught Referenceerror: _total is isn't defined return
 this.name;
}

var McDonald = new restaurant (' McDonald ');
Console.log (Mcdonald.getname ()); ' McDonald '
mcdonald.buy ();
Console.log (Mcdonald.gettotal ()); 9

A more flexible way of merging
Using module mode we can call multiple times, and each time it is done, it will be destroyed. With constructors, you can pass in some initialized data, but you cannot access private member properties in public methods, and if there are many public methods that require access to private data, we all write with privileged methods, which in the end will take many unnecessary methods to each instance. Therefore, the combination of the two can be complementary, and the combination of the way is very simple

var restaurant = (function () {
 //private Property
 var _total = ten;

 Private method
 function _buyfood () {
 _total--
 }

 constructor function
 Restaurant (name) {
 this.name = name;
 This.gettotal = function () {return
 _total;
 }
 }

 Restaurant.prototype.buy = function () {
 console.log (_total);//
 _buyfood ();
 }

 Restaurant.prototype.getName = function () {return
 this.name;
 }

 return restaurant;
}) ();

var McDonald = new restaurant (' McDonald ');
Console.log (Mcdonald.getname ()); ' McDonald '
mcdonald.buy ();
Console.log (Mcdonald.gettotal ()); 9

The above is the entire content of this article, small series just summed up a small part of it, there are many did not mention the knowledge point, we can explore their own research, I hope this article can help beginners.

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.