Basic js knowledge (public, private, and privileged) and basic js knowledge

Source: Internet
Author: User

Basic js knowledge (public, private, and privileged) and basic js knowledge

Although the theme involved in this article is very basic, it seems to many people to be a little trick, but it is a comprehensive topic in the basic knowledge of JavaScript. This will involveEncapsulation, prototype, constructor, closure, and immediate execution expression of Object AttributesAnd other knowledge.

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

// Var Restaurant = {name: 'mcdonald 'in the object, // public method getName: function () {return this. name ;}} // function Person (name, age) {this. name = name; this. age = age; // public method this. getName = function () {return this. name ;}} // The Person in the prototype. prototype. getAge = function () {return this. age ;}

Private MethodAndPrivileged Methods
These two methods are generally discussed together because the privileged methods we define refer to the public methods that have the right to access internal private attributes and private methods, private methods are externally invisible and inaccessible.

There are two ways to define an Object. One is to use Object Instantiation or Object expressions, and the other is to use constructors. The form of defining private and privileged methods in different ways is also different.

In the object
Here we use the Object expression to create an Object, add some attributes and methods, and then call it in static mode. The private data of the object is placed in an anonymous function that immediately executes the expression (IIFE. This means that this function exists only when it is called and is destroyed immediately after execution.

The method of creating private data in an object is called the module mode in the object mode (the object Creation Mode). Its basic format is as follows:

Var yourObject = (function () {// Private Attribute and method return {// public method and attribute }})();

In module mode, only public attributes and methods can be included in the returned object literal.

Var Restaurant = (function () {// Private Property var _ total = 10; // 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 use closures to indirectly use internal private variables, and initialize the Restaurant name (name.

In the constructor
When creating private methods in the module mode described above, there is no essential difference between public methods and privileged methods, the reason is that this concept is defined when the constructor is used to create private data.

It is convenient to define private attributes and methods in constructors. We do not need to use closures and can initialize data during calls.

Function Restaurant (name) {// Private Property var _ total = 10; // public property this. name = name; // Private method function _ buyFood () {_ total --;} // privileged method this. buy = function () {_ buyFood ();} this. getTotal = function () {return _ total ;}// public method. Note that private member _ totalRestaurant cannot be accessed here. prototype. getName = function () {console. log (_ total); // Uncaught ReferenceError: _ total is not defined return this. name;} var McDonald = new Restaurant ('mcdonald '); console. log (McDonald. getName (); // 'mcdonald 'McDonald. buy (); console. log (McDonald. getTotal (); // 9

Combined into one, more flexible way
The module mode can be called multiple times, and is destroyed after each execution. You can use the constructor to pass in some initialization data, but the private member attributes cannot be accessed in the public method. If many public methods need to access private data, we use all the privileged methods to write data. In the end, we will bring a lot of unnecessary methods to each instance. Therefore, the combination of the two can be complementary in length, and the combination method is also very simple.

Var Restaurant = (function () {// Private Property var _ total = 10; // Private method function _ buyFood () {_ total --;} // constructor function restaurant (name) {this. name = name; this. getTotal = function () {return _ total;} restaurant. prototype. buy = function () {console. log (_ total); // 10 _ 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 all the content of this article. The small Editor only summarizes a small part of it, and there are still many knowledge points that have not been mentioned. You can explore the research by yourself. I hope this article will be helpful to beginners.

Related Article

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.