Research on class attributes in javascript

Source: Internet
Author: User

This article mainly analyzes the attributes of javascript. Because javascript is an object-based language and does not have the concept of classes, there are many names for the definition of javascript classes, for example, prototype objects and constructor functions are all classes in javascript. For example, function Person () {} var p = new Person (); The Person here can be considered as a class, while p is an instance of this class, which can also be called an object. Here we mainly analyze the four attributes in js. Private attributes are defined in the class with var Declaration, that is, var propertyName = sonmeValue. They can only be accessed in this class and cannot be inherited or accessed in the prototype method. Privileged attribute refers to the use of the this keyword in the class or in the constructor (the same thing in js), that is, this. propertyName = someValue. This attribute can be accessed in the class. It can be accessed in the prototype method, and can be accessed in the class object. It can even be accessed when it is inherited using call or apply. common attribute, which is defined by ClassName. prototype. propertyName = someValue is defined. If this attribute is not defined in the class, that is, there is no life-heavy privileged attribute, it can be called in the object when the privileged attribute is accessed, sub-classes inherited by prototype can also be accessed. Static attributes are defined by ClassName. propertyName = someValue, which is equivalent to a namespace and can be accessed from outside the class. Example 1: function Person () {var private_name = "James"; // Private Property var private_age = 10; // Private property this. privilege_name = ""; // privileged attribute this. privilege_age = 9; // privileged attribute} Person. prototype. public_name = "xiaofang"; // public property Person. prototype. public_age = 8; // total property Person. static_name = "Xiao Li"; // Static Property Person. static_age = 7; // Static Property var pp = new Person (); pp. name = 'wang '; // Static Property pp. age = 6; // static attributes are defined in this example. Note that in static attributes, the Person and pp are both Object instances, as shown in the code execution result below. Console. log (Person instanceof Object, pp instanceof Object); // true so static attributes can be defined. The following describes the access permissions of these attributes. Example 2: function Person () {var private_name = 'xiaoming '; var private_age = 10; this. privilege_name = 'small red'; this. privilege_age = 9; // defines a privileged method this. showPrivilegeName = function () {console. log (private_name); // private_name is not defined. it indicates that private attributes can be accessed in privileged methods. console. log (this. privilege_name); // output: "Xiaohong ". The privileged attribute can access console. log (this. public_name) in the privileged method; // The output is "xiaofang ". It indicates that you can access console. log (Person. static_name) in the privileged method. // The output is "Xiao Li ". Description: The static attributes of the class can be accessed in the privileged method} Person. prototype. public_name = 'xiaofang '; Person. prototype. public_age = 8; Person. static_name = 'lil'; Person. static_age = 7; var pp = new Person (); pp. name = 'wang '; pp. age = 6; // define a prototype method Person. prototype. showName = function () {// console. log (private_name); // private_name is not defined. this indicates that private attributes cannot be accessed in the prototype method. console. log (this. privilege_name); // output: "Xiaohong ". This indicates that the privileged attribute can access console. log (this. public_name) in the prototype method; // The output is "xiaofang ". Indicates that you can access console. log (Person. static_name) in the prototype method. // The output is "Xiao Li ". Description: The static attributes of the class can be accessed in the prototype method} pp. showPrivilegeName (); pp. showName (); console. log (pp. private_name); // The undefined private attribute cannot access the console in the instantiated object. log (pp. privilege_name); // output: "Xiaohong ". The privileged attributes can be accessed by console. log (pp. public_name) in the prototype method; // The output is "xiaofang ". Indicates that you can access console. log (Person. static_name) in the prototype method. // The output is "Xiao Li ". The static attributes of the class can be accessed by console. log (pp. name) in the prototype method; // The output is "John ". It means that the static attributes of the instance object can be accessed in the prototype method. If the privileged attributes and the Public attributes have the same names, the privileged attributes are accessed. If the privileged attributes do not exist, the common attributes are accessed. In addition, if the delete object attribute is deleted, you can continue to access the common attributes after deletion. Static attributes of an object can only be accessed by this object, and other objects of the class cannot be accessed. Example 3: priority problem between privileged attributes and object static attributes function Person () {this. name = 'lily';} var p1 = new Person (); p1.name = 'red'; p1.age = 10console. log (p1.name); // small red console. log (p1.age); // 10 delete p1.nameconsole. log (p1.name); // undefined Example 4 priority problem between privileged attributes and object static attributes function Person () {this. name = 'lily';} var p1 = new Person (); p1.name = 'red'; p1.age = 10console. log (p1.name); // small red console. log (p1.age); // 10 delete p1.nameconsole. log (p1.name ); // Undefined: if an object is created and a static property with the same name as the privileged property is created for the object, the value of the privileged property will be overwritten. If delete is used, then the access is undefined. Summary: The object's static properties can only be accessed by the object itself. Priority: Static Properties of the Object> privileged properties of the class> common properties. The static attributes of the object overwrite the privileged attributes of the class, while the privileged attributes of the class do not overwrite the common attributes. delete is used to delete the attribute of the object, you cannot delete the common attributes of a class. Static class attributes and methods can be accessed everywhere, that is, they are equivalent to namespaces. For common attributes of a class, privileged attributes can be accessed externally. For private attributes and methods, only internal classes can be used. The common attributes and methods of a class can be the privileged attributes and methods of the class, but not the private attributes and methods of the class.

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.