Home >Web Front-end >JS Tutorial >Summary of examples of basic concepts such as objects, public members, and private members in JavaScript
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.
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:
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; }
Construct a new object:
var myContainer = new Container('abc');
Then, the public variable myContainer.member has the value 'abc'.
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; }
Then we can call this method:
myContainer.stamp('def')
Return 'abcdef'.
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; }
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; }
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.
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; } }; }
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.
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.
function Constructor(...) { this.membername = value; } Constructor.prototype.membername = value;
function Constructor(...) { var self = this; var membername = value; function membername(...) {...} }
Note: This code:
function membername(...) {...}
is actually an abbreviation of the following code
var membername = function membername(...) {...};
function Constructor(...) { this.membername = function (...) {...}; }
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!