js object oriented public private static properties and methods


Currently, javascript is very popular. For web developers, javascript is a necessary language to master. However, with the popularity and use of frameworks such as jquery, many people lack a deep understanding of native javascript, are used to the functional editing style, and are always confused about closures and prototypes. For the poor use of js object-oriented, to understand js object-oriented, you must first understand what js public methods, privileged methods, static methods are

Methods/steps

1. Public properties and methods

function User(name,age){
  this.name = name;// Public attribute
  this.age = age;
}
User.prototype.getName = function(){// Public methods
  return this.name;
}
var user = new User('fire Sub sea ',26);
console.log(user.getName());//output:fire Sub sea

2. Private properties and methods

function User(name,age){
  var name = name;// Private property
  var age = age;
  function alertAge(){// Private methods
     alert(age);
  }
  alertAge(age); // The pop-up 26
}
var user = new User('fire Sub sea ',26);

3. Static properties and methods

In php, the methods that can be called without instantiation are called static methods, and js is the same. Without instantiation, the methods and properties of an object can be called using the new operation.

function User(){}
User.age = 26;// Static attributes
User.myname = 'fire Sub sea ';
User.getName =function(){// A static method

  return this.myname;// If I use this.name , the return is going to be User All of them have switched over myname .
}
console.log(User.getName());//output:fire Sub sea

4. Privileged methods

function User(name,age){
  var name = name;// Private property
  var age = age;
  this.getName = function(){ // Privilege method
     return name;// Private properties and methods cannot be used this call
  }
}
var user = new User('fire Sub sea ',26);
console.log(user.getName());//output:fire Sub sea

5. A static class

For static methods and static properties, we don’t need to create them as we did in step 3. If you’ve seen my article “how to make js images”, you can create them using literal values.

var user = {
  init:function(name,age){
   this.name = name;
   this.age = age;
  },
  getName:function(){
   return this.name;
 }
}
user.init('fire Sub sea ',26);
console.log(user.getName());//output:fire Sub sea

6. Rules for calling public methods

To call a public method, we must first instantiate the object

Public methods do not call public properties and privileged methods by this, and static methods and properties cannot be called by this. They 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 attribute
  this.age = 26;
  this.do = function(){// Privilege 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());// Calling privileged methods
}
User.prototype.alertEat = function(food){
  alert(User.eat(food));// You can only call static methods through the object itself
  //alert(this.ear(food)) This call will cause an error :this.eat is not a function
}
var user = new User();
user.alertAge();//alert:26
user.alertDo();//alert:fire Child learn js
user.alertEat(' The instant noodles ')//alert: Only instant noodles for dinner

7. Rules for calling static methods

When using static methods, the object can be called without instantiating the object. The object instance cannot call the static method of the object, but can only call the static properties and methods of the instance itself

function User(){}
User.age = 26;// Static attributes
User.myname = 'fire Sub sea ';
User.getName =function(){// A static method

  return this.myname;
}
var user = new User();
console.log(user.getName);//TypeError: user.getName is not a function
user.supper = ' The instant noodles ';
user.eat = function(){
 return ' Dinner only '+this.supper;
}
user.eat();// Only instant noodles for dinner

Static methods cannot call public properties, public methods, private methods, private properties, privileged methods, and stereotype properties

function User(){
    this.myname = 'fire Sub sea ';// Public attribute
    this.age = 26;
    this.do = function(){// Privilege method
      return this.myname+' learning js';
    }
}
User.prototype.alertAge = function(){// Public methods, also known as prototype methods
  alert(this.age);
}
User.prototype.sex = ' male ';// The prototype property
User.getName= function(){// A 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. Rules for calling privileged methods

Privileged methods call public methods and public properties through this, static methods and properties through the object itself, and private properties and private methods directly within the method body

function User(girlfriend){
   var girlfriend = girlfriend;
   function getGirlFriend(){
     return ' My girlfriend '+girlfriend+' Is beauty! ';
   }
  this.myname = 'fire Sub sea ';// Public attribute
  this.age = 26;
  this.do = function(){// Privilege method
    return this.myname+' learning js';
  }
  this.alertAge = function(){
   this.changeAge();// Privileged methods call public methods
    alert(this.age);
  }
  this.alertGirlFriend = function(){
   alert(getGirlFriend());// Calling a private method
  }
}
User.prototype.changeAge = function(){
  this.age = 29;
}
var user = new User(' so-and-so ');
user.alertAge();//alert:29
user.alertGirlFriend();//alert: My girlfriend so-and-so is a beautiful girl!

9. Private methods

The private methods and properties of the object are not accessible externally, and the public methods, public properties and privileged methods of the object cannot be called by this within the method

function User(girlfriend){
   var girlfriend = girlfriend;
  this.myname = 'fire Sub sea ';// Public attribute
  this.age = 26;
  function getGirlFriend(){
   //this.myname ;// At this time this Point to the window Object, not User Object,
    // this.myname = 'fire Sub sea ', At this time this Points to the getGirFriend The object.
  // If through this Call the getGirFriend Methods that don't exist, properties, this Can point to window  Object, only this Call the getGirlFriend Existing methods and properties, this To be specified getGirlFriend;
     alert(User.eat(' Instant noodles '));//alert Only instant noodles for dinner
  }
  this.do = function(){// Privilege method
    return this.myname+' learning js';
  }
  this.alertAge = function(){
   this.changeAge();// Privileged methods call public methods
    alert(this.age);
  }
  this.alertGirlFriend = function(){
   getGirlFriend();// Calling a private method
  }
}
User.eat = function(supper){
 return ' Dinner only '+supper;
}
var user = new User(' so-and-so ');
user.alertGirlFriend();

That’s all for this article, I hope you enjoy it.