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

Source: Internet
Author: User

1. Public properties and public methods

unction User (name,age) {    this. Name = name;   Public Property this    . Age =function() {// public method    Returnthis. Name;} var New User (' Fire sub-Sea ', ' Console.log '); (user.getname ()); // Output:fire Zihai

2. Private properties and methods

function User (name,age) {    var name = name;   Private attribute    var age = Age ;    function alertage () {// Private method          alert (age);   }    // pop Up }varnew User (' Fire sub-Sea ', 26);

3. Static properties and methods

A method that can be called without instantiation in JS is called a static method.

function= 26; // Static Properties User.myname = ' Fire sub-Sea' =function() {// static method    returnthis. MyName // If the this.name is used here, the user will be returned, and all the myname are replaced. }console.log (User.getname ()); // Output:fire Zihai

4. Privileged Methods

 function   User (name,age) { var  name = name; //     private property     var  age = age;  function  () {//  Privileged method  return  Name;//  private properties and methods cannot use this call   var  user = new  User (' Fire sub-Sea ', 26 // output:fire Zihai  

5. Static Class

For static methods and static properties, we don't need to create them in the same way as in the third step, which can be created in a literal manner.

var user = {   init:function(name,age) {      this. Name= name;       this. Age = Age ;   },   getName:function() {      return  this. Name;}  } User.init (' Fire sub-Sea '), Console.log (User.getname ()); // Output:fire Zihai

6. Calling rules for public methods

Call the public method, we must instantiate the object first. In public methods, public properties and privileged methods cannot be called by this method, and static methods and properties cannot be called with this, which must be called by the object itself, that is, the object name. Public methods cannot also call private methods.

functionUser () { This. myname = ' Fire sub-Sea ';//Public Properties     This. Age = 26;  This. Do=function(){//Privileged Methods        return  This. myname+ ' Learning 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

7. Calling rules for static methods

When you use a static method, you can call it 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.

functionUser () {}user.age= 26;//Static PropertiesUser.myname = ' Fire sub-Sea '; 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 noodles

Static methods cannot invoke public properties, public methods, private methods, private properties, privileged methods, and prototype properties

functionUser () { This. myname = ' Fire sub-Sea ';//Public Properties        This. Age = 26;  This. Do=function(){//Privileged Methods            return  This. myname+ ' Learning 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

8. Calling rules for privileged methods

The privileged method calls the public method, the public property through this, and calls the static methods and properties through the object itself, calling the private property and the private method directly within the method body.

functionUser (girlfriend) {varGirlfriend =girlfriend; functionGetgirlfriend () {return' My girlfriend ' +girlfriend+ ' is a beauty! ‘; }     This. myname = ' Fire sub-Sea ';//Public Properties     This. Age = 26;  This. Do=function(){//Privileged Methods        return  This. myname+ ' Learning 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 = 29;}varuser =NewUser (' xxx '); User.alertage ();//alert:29User.alertgirlfriend ();//alert: My girlfriend xxx is a beauty! 

9. Private methods

The private methods and properties of the object are not accessible externally, and within the method are not the public methods, public properties, privileged methods that can invoke the object.

functionUser (girlfriend) {varGirlfriend =girlfriend;  This. myname = ' Fire sub-Sea ';//Public Properties     This. Age = 26; functionGetgirlfriend () {//this.myname;//The Window object at this point is not a 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 face '));//alert: Only instant noodles for dinner    }     This. Do=function(){//Privileged Methods        return  This. myname+ ' Learning 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 ();

JS public, private, static properties and methods differ

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.