Javascript-object-oriented (1) (common methods, private methods, and privileged methods)

Source: Internet
Author: User

Recently, I have provided some JavaScript object-oriented content on the Internet. Summarize the things summarized by other experts with your own understanding:

 

Private Method: A private method can be set to all the attributes inside the category, that is, private and public attributes. However, private methods cannot be called outside the class.

Private method Syntax:

 
FunctionMyclass (){4VaRPrivate_attribute =Initial_value;5FunctionPrivate_method (){}6VaRPrivate_method2 =Function(){}7}

Instance showpet () is a private Method

  var  PET =  function   () {
var temp = "" // Private variables can only be accessed within the scope of a function or object function showpet () {alert (" 123 ")} showpet (); // private methods can be used within the scope of the function. }showpet (); /// error pet. showpet () /// still cannot be called like this var penguin = New PET () /// instantiate 1 Pet objects penguin. showpet () /// sorry, this still cannot be called.

 

Public method:

1. Public methods can be called outside the class,

2. However, it cannot be a private attribute of the category class.

3. Public methods must be added either inside or outside the class through the prototype attribute of the class.

Public method Syntax:

10FunctionMyclass (){11This. Public_attribute =Initial_value;12This. Prototype. public_method =Function(){}13}14 myclass. Prototype. public_attribute2 =Initial_value;15 myclass. Prototype. public_method2 =Function(){}

Instance:

 VaR PET = Function  (){  Function Showname (){ //  Private Method Alert ( This  . Name )}  This . Show = Function (){ //  If you do not understand it here, please note that this method will be introduced below.  Showname ();}}
Pet. Prototype. setname = Function (STR) {name = STR ;}
VaR Penguin =New PET () penguin. setname ( "Penguin "); // The name of the added instance is penguin. Penguin. Show (); // Penguin Penguin. setname ("wind "); // The name of the added instance is wind. Penguin. Show (); // Wind

 

Privileged method:

1. Privileged methods can be called outside the class,

2. But it can be a private property of the category and a public property of the category, and it can be barely considered a special public method.

3. But it is different from the Declaration and definition of the public method above. Privileged methods must be declared and defined within the class.

Privileged method Syntax:

 
18FunctionMyclass (){19This. Privileged_method =Function(){}20}

Instance

 VaR PET = Function  (){  Function Showname (){//  Private Method Alert ( This  . Name )}  This . Show = Function (){ //  Use the this keyword to define a privileged method. Showname (); //  Access private methods in privileged methods;  } Pet. Prototype. setname = Function  (STR) {name = STR ;} VaR Penguin = New PET (); //  Instantiate a pet object Penguin. setname ("Penguin "); //  Call Public method Modification Penguin. Show (); //  When you call a privileged method to access a private method, name is displayed. 

 

Source:

Http://www.cnblogs.com/qiantuwuliang/archive/2009/10/17/1584998.html

Http://www.cnblogs.com/playpc/articles/1863267.html

 

Below are some of my own understandings: Through the above learning, combined with my own books. The public, private, and privileged permissions are understood as follows:

Public method: all objects instantiated through this class have or can be used together. The common methods are generally put in the "prototype object". If they are put in the constructor, the common methods are created again.

 

Private method: it cannot be called externally.

 

Privileged method: the closure principle is used to allow internal functions to access variable objects (private variables and private methods) of external functions through the scope chain ). (Scope chain, closure, variable object; these three are in Javascript advancedProgramAre explained)

 

 

 

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.