There are generally two common ways to create objects in JS . One, the object literal. Two, the constructor function method.
The object literal method is simple, so you don't have to repeat it here.
for an object, there are attributes(Attribution) and methods, whichcollectively refer to the properties and methodsof an object as a property of the object.
For objects created with object literals, only public attributes. The properties and methods of the object can be arbitrarily accessed and modified by the point operator, the object is easily destroyed and the object's encapsulation is poor.
the object created by the constructor method can provide some encapsulation. Implementation is similar to C + +,Java Private methods, constants, static classes and other features.
Let's look at a typical example
//Person classfunctionPerson () {//Private Features varAge= ' 23 '; //Privileged Attributes This. Name= ' Bronvis ' This. sayage=function() {alert (' Age: ' +Age ); }}//Public featuresPerson.prototype={location:' Hangzhou 'saylocation:function() {alert (' Location: ' + This. Location); }}//public static characteristics of a classPerson.country= ' China ';varobj=NewPerson ();//static properties of ObjectsObj.sex= ' Male ';
The object created for this constructor method.
you can discover the properties and methods of an object created in 4 ways. which can be divided into two categories, one, the characteristics of the object itself. Second, all objects share the prototype feature.
One, the object's own characteristics
1,var keywords
private features. the accessibility of a variable scoped by a function is limited to the inside of the function, so that these attributes can only be accessed within the object.
2,this keywords
Privileged attributes. The attributes created with the This keyword can be accessed by any external object through the point operator, but can access the private properties of the object compared to the statically added properties of the object. Therefore, it is often called a privileged attribute.
3, object static Properties
public attribute. (private attributes cannot be accessed compared to privileged attributes)
Second, prototype features
public attribute. the difference between the properties created with the object's static properties is primarily the memory footprint. The prototype feature is that multiple objects share the same piece of memory, whereas for object static properties each object consumes memory.
In the example above, only the static public properties of the class are created, and the following example creates a private static property of the class.
//implementing static private properties of a class by means of closuresvarPerson= (function() {//static private properties of the class varTime ; //returns a constructor return function() {//Private Features varAge= ' 23 '; //Privileged Attributes This. Name= ' Bronvis ' This. sayage=function() {alert (' Age: ' +Age ); }}) ();//public static characteristics of a classPerson.country= ' China ';
static private properties of classes are commonly used to implement C + +, constants in the Java language.
JS private features, public and privileged features