Home > Web Front-end > JS Tutorial > body text

Summary of examples of basic concepts such as objects, public members, and private members in JavaScript

伊谢尔伦
Release: 2017-07-27 11:00:06
Original
1992 people have browsed it

JavaScript is built on objects. Arrays are objects, functions are objects, and objects are of course objects. So what is an object? An object is a collection of name-value pairs. Names are strings, but values ​​can be strings, numbers, booleans, or objects (including arrays and functions). Objects are usually implemented using hash tables so that values ​​can be retrieved quickly.

If the value is a function, we can treat it as a "method". When a method of the object is executed, the variable this is set to the object itself. In this way, the method can access the instance of the object through the this variable.

Objects can be created through "constructor". A constructor is a function that holds an initialized object. Constructors provide features and functionality similar to those provided by "classes" in other languages, including static variables and methods.

Public

All members of the object are public members. Any function can access, modify or delete these members, and of course can add new members. There are two main ways to add members to an object:

Through the constructor

This method is generally used to initialize public variables of an object instance. The this variable of the constructor is used to add members to the object:

function Container(param) {
  this.member = param;
}
Copy after login

Construct a new object:

var myContainer = new Container('abc');
Copy after login

Then, the public variable myContainer.member has the value 'abc'.

Through prototype (prototype)

This method is usually used to add public methods. When the object itself is searched for a member but not found, the constructor's prototype member is used. This prototype mechanism implements the so-called "inheritance" of object-oriented and also saves memory. To add a method to all objects created from the same constructor, we only need to add a function to the prototype of the constructor:

Container.prototype.stamp = function (string) {
  return this.member + string;
}
Copy after login

Then we can call this method:

myContainer.stamp('def')
Copy after login

Return 'abcdef'.

Private

Private members are created by the constructor. Usually variables and function parameters declared with var in the constructor become private members.

function Container(param) {
  this.member = param;
  var secret = 3;
  var self = this;
}
Copy after login

This constructor creates three private instance variables: param, secret and self.

function Container(param) {
  function dec() {
   if (secret > 0) {
     secret -= 1;
     return true;
   } else {
     return false;
   }
  }

  this.member = param;
  var secret = 3;
  var self = this;
}
Copy after login

The private method dec will check the instance variable secret. If it is greater than 0, decrement it by 1 and return true; if it is less than 0, return false. This achieves the function that the dec function of the object created by this builder can only be used three times.

By convention, we create a private variable self. Private methods can access the object itself. But this is only a stopgap measure, because there is a bug in the "ECMAScript Language Specification" that causes the this variable of the internal function to be set to an incorrect value.

Privileges

Privileged methods are created through this inside the constructor.

function Container(param) {
  function dec() {
   if (secret > 0) {
     secret -= 1;
     return true;
   } else {
     return false;
   }
  }

  this.member = param;
  var secret = 3;
  var self = this;

  this.service = function () {
   if (dec()) {
     return self.member;
   } else {
     return null;
   }
  };
}
Copy after login

Service is a privileged method. The first three calls to myContainer.service() will return 'abc', after which it will return null. The service accesses the private variable secret by calling the private method dec. For other objects and methods, the service can be accessed, but private members cannot be directly accessed.

Closure

This pattern of public, private, and privileged members exists due to an intrinsic mechanism of JavaScript: closures. This means that an inner function can always access the variables and parameters of its outer function, even if the outer function has returned. This is a very powerful feature of the JavaScript language. There are currently no books on JavaScript programming that show how to take advantage of it, and they don't even mention it.

Private and privileged members can only be created when the object is initialized, while public members can be added at any time.

Mode

Public
function Constructor(...) {
  this.membername = value;
}
Constructor.prototype.membername = value;
Copy after login
Private
function Constructor(...) {
  var self = this;
  var membername = value;
  function membername(...) {...}
}
Copy after login

Note: This code:

function membername(...) {...}
Copy after login

is actually an abbreviation of the following code

var membername = function membername(...) {...};
Copy after login
privilege
function Constructor(...) {
  this.membername = function (...) {...};
}
Copy after login

The above is the detailed content of Summary of examples of basic concepts such as objects, public members, and private members in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!