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 in JS, the privileged method, static method method/Step
  1. 1

    Public properties and Public methods

    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 ', 26);

    Console.log (User.getname ());//output:fire zi Hai

  2. 2

    Private properties and methods

    function User (name,age) {

    var name = name;//Private Property

    var = age;

    function Alertage () {//Private method

    alert (age);

    }

    Alertage (age); Pop up 26

    }

    var user = new User (' Fire sub-Sea ', 26);

  3. 3

    Static Properties and methods

    In PHP, a method that can be called without an instantiation is called a static method, and JS is the same, without an instantiation, that is, the method and properties of the object can be invoked by using the new operator to manifest the object.

    function User () {}

    User.age = 26;//static property

    User.myname = ' Fire sub-Sea ';

    User.getname =function () {//static method

    Return this.myname;//If the this.name is used here, the user will be returned, all using MyName,

    }

    Console.log (User.getname ());//output:fire zi Hai

  4. 4

    Privileged methods

    function User (name,age) {

    var name = name;//Private Property

    var = age;

    This.getname = function () {//Privileged method

    Return name;//private properties and methods cannot be called with this

    }

    }

    var user = new User (' Fire sub-Sea ', 26);

    Console.log (User.getname ());//output:fire zi Hai

  5. 5

    Static class

    For static methods and static properties, we do not need to create the same as in the third step, if the Netizen read my "JS how to Make Picture Carousel", know can use literal way to create.

    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. 6

    Calling rules for public methods

    Calling the public method, we must first instantiate the object

    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 also cannot call private methods

    function User () {

    This.myname = ' Fire sub-Sea ';//Public property

    This.age = 26;

    This.do = function () {//Privileged method

    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 ());//Invoke Privileged method

    }

    User.prototype.alertEat = function (food) {

    alert (user.eat);//static method can only be called through the object itself

    Alert (this.ear) Such a call would have an error: This.eat is not a function

    }

    var user = new User ();

    User.alertage ();//alert:26

    User.alertdo ();//alert:fire Zihai Learning JS

    User.alerteat (' instant noodles ')//alert: Only instant noodles for dinner

  7. 7

    Calling rules for static methods

    When using 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

    function User () {}

    User.age = 26;//static property

    User.myname = ' Fire sub-Sea ';

    User.getname =function () {//static method

    return this.myname;

    }

    var user = new User ();

    Console.log (user.getname);//typeerror:user.getname is not a function

    User.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

    function User () {

    This.myname = ' Fire sub-Sea ';//Public property

    This.age = 26;

    This.do = function () {//Privileged method

    Return this.myname+ ' Learning JS ';

    }

    }

    User.prototype.alertAge = function () {//public method, also called prototype method

    alert (this.age);

    }

    User.prototype.sex = ' male ';//prototype properties

    User.getname= function () {//static method

    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. 8

    Calling rules for privileged methods

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

    function User (girlfriend) {

    var girlfriend = girlfriend;

    function Getgirlfriend () {

    Return ' My girlfriend ' +girlfriend+ ' is a beauty! ‘;

    }

    This.myname = ' Fire sub-Sea ';//Public property

    This.age = 26;

    This.do = function () {//Privileged method

    Return this.myname+ ' Learning JS ';

    }

    This.alertage = function () {

    This.changeage ();//Privileged method calls public method

    alert (this.age);

    }

    This.alertgirlfriend = function () {

    Alert (Getgirlfriend ());//Call Private method

    }

    }

    User.prototype.changeAge = function () {

    this.age = 29;

    }

    var user = new user (' xxx ');

    User.alertage ();//alert:29

    User.alertgirlfriend ();//alert: My girlfriend xxx is a beautiful girl!

  9. 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.

    function User (girlfriend) {

    var girlfriend = girlfriend;

    This.myname = ' Fire sub-Sea ';//Public property

    This.age = 26;

    function Getgirlfriend () {

    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 method

    Return this.myname+ ' Learning JS ';

    }

    This.alertage = function () {

    This.changeage ();//Privileged method calls public method

    alert (this.age);

    }

    This.alertgirlfriend = function () {

    Getgirlfriend ();//Call Private method

    }

    }

    User.eat = function (supper) {

    Return ' Dinner only ' +supper;

    }

    var user = new user (' xxx ');

    User.alertgirlfriend ();

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.