The difference between JS public, private, and static properties and methods

Source: Internet
Author: User

Now, JavaScript is great, and for Web developers, JavaScript is a must-have language, but with the popularity and use of frameworks such as jquery, many people lack in-depth understanding of native JavaScript and are accustomed to functional editing styles for closures , prototypes are always unclear. For JS object-oriented crappy use, and to understand JS object-oriented, we must first understand what is the public method JS, privileged methods, static methods

Public attribute and public method function User (name,age) {    this.name = name;//Public Property    this.age = age;} User.prototype.getName = function () {//Public method   return this.name;} var user = new User (' Fire sub-Sea ', "Console.log"); User.getname ());//output:fire zi Hai

  private properties and method function User (name,age) { var  name = name; //     private property    var  age = age; function Alertage () { //  Private method  Span style= "color: #000000;"   > alert (age); } alertage (age);  //  pop-up   var  user = new  User ( '  
  static properties and methods in PHP, the method that can be called without instantiation is called a static method, and JS is the same, without instantiation, that is, using the new operator to manifest the object, you can invoke the object's methods and properties. function User () {}user.age  = 26 ; //  static property  user.myname  =  "  Fire Zihai   "  =function () {//  static method  return  this . Myname;//  If you use this.name here, you will return the user, all using myname instead,   // output:fire Zihai  
Privileged Method function User (name,age) {varname = name;//Private Properties    varAge =Age ;  This. GetName = function () {//Privileged Methods          returnName//private properties and methods cannot be called with this   }}varuser =NewUser ('Fire Zihai', -); Console.log (User.getname ());//Output:fire Zihai
  static class for static methods and static properties, we do not have to create the same as in the third step, if the Netizen read my "JS how to Make Picture Carousel", I know you can use the literal way to create.  var  user = {init:function (name,age) {this . Name = name;     return  this
      .name; }}user.init (  fire Zihai   ' , 26  ); Console.log (User.getname ());  // output:fire Zihai  
The calling rule of the public method calls the public method, we must first instantiate the object by not calling the public property and the privileged method in the public method, not using this to call the static method and the property, must be cut through the object itself called, that is, the object name. The public method cannot also call the Private method function User () { This. myname ='Fire Zihai';//Public Properties     This. Age = -;  This. Do= function () {//Privileged Methods        return  This. myname+'Learn JS'; }}user.eat=function (food) {return 'Dinner Only'+Food ;} User.prototype.alertAge=function () {alert ( This. age);} User.prototype.alertDo=function () {alert ( This. Do());//invoking Privileged methods}user.prototype.alerteat=function (food) {alert (user.eat);//static methods can only be called through the object itself//alert (this.ear) Such a call would have an error: This.eat is not a function}varuser =NewUser (); User.alertage ();//alert:26user.alertdo ();//alert:fire Zihai Learning JSUser.alerteat ('Instant noodles')//alert: Only instant noodles for dinner
when a static method's call rule uses a static method, it can be called without instantiating the object, the object instance cannot invoke the static method of the object, only the static properties and methods of the instance itself can be called function User () {}user.age= -;//Static PropertiesUser.myname='Fire Zihai'; User.getname=function () {//Static Methods    return  This. myname;}varuser =NewUser (); Console.log (user.getname);//TypeError:user.getName is not a functionUser.supper='Instant noodles'; User.eat=function () {return 'Dinner Only'+ This. Supper;} User.eat ();//Dinner only instant noodlesstatic methods cannot invoke public properties, public methods, private methods, private properties, privileged methods, and prototype properties function User () { This. myname ='Fire Zihai';//Public Properties        This. Age = -;  This. Do= function () {//Privileged Methods            return  This. myname+'Learn JS'; }}user.prototype.alertage= function () {//public methods, also called prototype methodsAlert ( This. age);} User.prototype.sex='male';//Prototype PropertiesUser.getname= function () {//Static Methods    return  This. MyName;} User.getage=function () { This. Alertage ();} User.getdo=function () {return  This. Do();}//Console.log (User.getname ())//undefined//Console.log (User.getdo ());//TypeError:this.do is not a function//Console.log (User.getage ())//TypeError:this.alertAge is not a function
Call rules for privileged methods the privileged method calls the public method, the public property through this, calls the static method and property through the object itself, calls the private property directly in the method body and the Private method function User (girlfriend) {varGirlfriend =girlfriend; function Getgirlfriend () {return 'My girlfriend'+girlfriend+'It's beautiful! '; }     This. myname ='Fire Zihai';//Public Properties     This. Age = -;  This. Do= function () {//Privileged Methods        return  This. myname+'Learn JS'; }    This. Alertage =function () { This. Changeage ();//Privileged methods call public methodsAlert ( This. Age); }    This. Alertgirlfriend =function () {alert (Getgirlfriend ());//Calling Private Methods}}user.prototype.changeage=function () { This. Age = in;}varuser =NewUser ('XXX'); User.alertage ();//alert:29user.alertgirlfriend ();//alert: My girlfriend xxx is a beauty! 
Private method objects are private methods and properties that are not accessible externally, and within the method are not able to invoke the public method of the object, public properties, and the function User of the privileged method (girlfriend) {varGirlfriend =girlfriend;  This. myname ='Fire Zihai';//Public Properties     This. Age = -; function Getgirlfriend () {//This.myname;//This is the window object that this is pointing to, not the user object,//this.myname = ' Fire sub-Sea ', at this point this is the Getgirfriend object. //If you call a method that does not exist in Getgirfriend through this, the this will point to the Window object, and this will only specify getgirlfriend if this invokes the method and property Getgirlfriend exists .alert (User.eat ('Bubble Noodles'));//alert: Only instant noodles for dinner    }     This. Do= function () {//Privileged Methods        return  This. myname+'Learn JS'; }    This. Alertage =function () { This. Changeage ();//Privileged methods call public methodsAlert ( This. Age); }    This. Alertgirlfriend =function () {getgirlfriend ();//Calling Private Methods}}user.eat=function (supper) {return 'Dinner Only'+supper;}varuser =NewUser ('XXX'); User.alertgirlfriend ();

Reference: http://jingyan.baidu.com/article/d5c4b52bc2f71dda570dc575.html

The difference between JS public, private, and static properties and methods

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.