Attribute analysis of classes in JavaScript

Source: Internet
Author: User
Tags definition true true

This article focuses on the JavaScript properties of the analysis, because JavaScript is an object-based language, there is no concept of the class, so the definition of JavaScript class is a lot of names, examples of prototype objects, constructors, etc., They all refer to classes in JavaScript.

For example: function person () {} var p = new person (); The person here can be considered as a class, and P is an instance of this class, which can also be called an object. Here the main analysis JS inside four kinds of attributes.

A private property, which is defined in the class with Var declared, that is, var propertyname = Sonmevalue, which can only be accessed within this class, cannot be inherited, and cannot be accessed in a prototype method.

Privileged attributes, which are in the class or in the constructor (JS is the same thing), use the This keyword, this.propertyname = somevalue, which is accessible in the class, accessible in the prototype method, and accessible in the object of the class. It can be accessed even with call or apply inheritance.

A common attribute, defined by Classname.prototype.propertyname=somevalue, that is not defined in a class, that is, a privileged property that does not have a command, can be invoked in an object when the privileged property is accessed. Subclasses that are inherited through prototype can also be accessed.

Static properties, which are directly classname.propertyname=somevalue to define, are equivalent to a namespace that can be accessed from within the class.

Example 1: various attribute definitions

function person () {
     var private_name = "xiaoming";//private Property
     var private_age = 10;//private Property
     This.privilege_name = "Little Red"; Privilege attribute
     this.privilege_age = 9;//Privileged Property
  }
    Person.prototype.public_name = "Small Fang";//Public Property
    Person.prototype.public_age = 8; Common attribute
    person.static_name = "Xiao Li";//static property
    Person.static_age = 7;//static property
      
    var pp = new person ();
    Pp.name = ' Xiao Wang '; static property
    Pp.age = 6;//static properties

In this example, the definition of these four attributes is indicated. Note the static properties, because both person and PP are instances of object, as can be seen from the following code execution results.

Console.log (person instanceof object,pp instanceof Object); True True

So you can define static properties.

The following is an analysis of the access rights for these properties.

Example 2: Access rights for various properties

function person () {var private_name = ' xiaoming ';   
  var private_age = 10;
  This.privilege_name = ' Little Red ';
      
    This.privilege_age = 9;             Defines a privileged method this.showprivilegename = function () {console.log (private_name);
    Private_name is not defined. Describes private properties that can be accessed in privileged methods.         Console.log (This.privilege_name); Output: "Little Red".            Explains that privileged properties can access Console.log (This.public_name) in privileged methods; Output: "Xiao Fang".        Description Common Properties can access Console.log (Person.static_name) in privileged methods; Output: "Xiao Li".
Describes the static properties of a class that can be accessed in a privileged method} Person.prototype.public_name = ' small Fang ';  
Person.prototype.public_age = 8;  
Person.static_name = ' Xiao Li ';   
Person.static_age = 7;
var pp = new person ();      
Pp.name = ' Xiao Wang ';
      
Pp.age = 6;         Defines a prototype method Person.prototype.showName = function () {//console.log (private_name);
  Private_name is not defined. Indicates that private properties cannot be accessed in the prototype method.         Console.log (This.privilege_name); Output: "Little Red".        Demonstrates that privileged properties can access Console.log (This.public_name) in a prototype method; Output: "Xiao Fang". Explains common properties you can access the console in a prototype method.Log (person.static_name); Output: "Xiao Li".
Describes the static properties of a class that can be accessed in the prototype method} pp.showprivilegename ();
Pp.showname ();            Console.log (Pp.private_name);         Undefined private properties cannot access Console.log (Pp.privilege_name) in an instantiated object; Output: "Little Red".            Demonstrates that privileged properties can access Console.log (Pp.public_name) in a prototype method; Output: "Xiao Fang".        Shows that common properties can access Console.log (Person.static_name) in a prototype method; Output: "Xiao Li".                 Describes the static properties of a class to access Console.log (Pp.name) in a prototype method; Output: "Xiao Wang". Describes the static properties of an instance object that can be accessed in a prototype method

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

Related Article

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.