JS Object-oriented public, private, static attributes and methods _javascript skills

Source: Internet
Author: User
Tags static class

Now, JavaScript is a big line, JavaScript is a necessary language for Web site developers, but with the popularity and use of frameworks such as jquery, many people lack a deep understanding of native JavaScript and are accustomed to functional editing style, for closures , the prototype is always not clear the way unknown. For JS object-oriented, and to understand the JS object-oriented, you must first understand JS in what is public method, privileged method, static method

Method/Step

1. Public property and public methods

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

2. Private properties and methods

function User (name,age) {
  var name = name;//Private Property
  var age = age;
  function Alertage () {//Private Method
     alert (age);
  }
  Alertage (age); Popup
= var user = new User (' Fire Sea ', 26);

3. Static properties and methods

In PHP, the method that can be called without instantiating is called static method, JS is same, need not instantiate, that is, the object is manifested with the new operator, the method and property of the object can be called.

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 returned will be user, all converted to MyName,
}
Console.log (User.getname ());//output:fire zi Hai

4. Method of Privilege

function User (name,age) {
  var name = name;//Private Property
  var age = age;
  This.getname = function () {//Privileged method return
     name;//private properties and methods cannot use this call
  }
var user = new User (' Fire sub-Sea ') , num);
Console.log (User.getname ());//output:fire zi Hai

5. Static Class

For static methods and static properties, we do not need to create the same as in the third step, if the user saw my "JS how to make a picture carousel," You know you can use the literal way to create.

var user = {
  init:function (name,age) {
   this.name = name;
   This.age = age;
  },
  getname:function () {return
   this.name
 }
} User.init (' Fire Sea ', num);
Console.log (User.getname ());//output:fire zi Hai

6. Rules of invocation of public methods

To invoke the public method, we must first instantiate the object

Public methods do not invoke the common and privileged methods in the public method, and you cannot use this to invoke static methods and properties, which must be called by the object itself, that is, the object name. Public methods also cannot invoke private methods

function User () {
  this.myname = ' fire Sub Sea ';//public attribute
  this.age =;
  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 (food));//static Method//alert (This.ear (food) can only be invoked through the object itself)
  : 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: dinner only instant noodles

7. Call rules for static methods

When you use a static method, you can invoke it without instantiating the object, and the object instance cannot invoke the static method of the object, only the static properties and methods of the instance itself

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 stereotype properties

function User () {
    this.myname = ' fire Sub Sea ';//public attribute
    this.age =;
    This.do = function () {//Privileged method return
      this.myname+ ' learning js ';
    }
}
User.prototype.alertAge = function () {//public method, also known as prototype Method
  alert (this.age);
}
User.prototype.sex = ' male ';//prototype Property
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. Calling rules for privileged methods

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

function User (girlfriend) {
   var girlfriend = girlfriend;
   function Getgirlfriend () {return 
     ' my girlfriend ' +girlfriend+ ' is a beauty! ';
   }
  This.myname = ' fire sea ';//public attribute
  this.age =;
  This.do = function () {//Privileged method return
    this.myname+ ' learning js ';
  }
  This.alertage = function () {
   this.changeage ();//Privileged method invokes public method
    alert (this.age);
  }
  This.alertgirlfriend = function () {
   alert (getgirlfriend ());//Calling Private
  method
}
} User.prototype.changeAge = function () {
  this.age =;
}
var user = new User (' So-and-so ');
User.alertage ();//alert:29
user.alertgirlfriend ()//alert: My girlfriend So-and-so is a beauty!

9. Private method

The private methods and properties of the object, which cannot be accessed externally, are not within the method that can this call the object's public method, the public property, the privileged method's

function User (girlfriend) {
   var girlfriend = girlfriend;
  This.myname = ' fire sea ';//public attribute
  this.age =;
  function Getgirlfriend () { 
   //this.myname;//This point to the Window object, not the user object,
    //This.myname = ' Fire Sub Sea ', This now points to the Getgirfriend object.
  //If a method that does not exist in Getgirfriend is invoked through this, this will point to the Window object, and this will be specified only if the method and property that Getgirlfriend exists is called Getgirlfriend ;
     alert (user.eat);//alert: Dinner only instant noodles
  }
  this.do = function () {//Privileged method return
    this.myname+ ' learning js ';
  }
  This.alertage = function () {
   this.changeage ();//Privileged method invokes public method
    alert (this.age);
  }
  This.alertgirlfriend = function () {
   getgirlfriend ();//Calling Private method
  }
}
user.eat = function (supper) { Return
 ' Dinner only ' +supper;
}
var user = new User (' So-and-so ');
User.alertgirlfriend ();

The above mentioned is the entire content of this article, I hope you can enjoy.

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.