Three kinds of members of JavaScript classes in Ajax

Source: Internet
Author: User
Tags add define object event listener functions variables variable variable scope
Ajax|javascript the public member of the implementation class

Any of the class members previously defined are in the category of public members, and any instance of the class exposes these properties and methods externally.

implementing a private member of a class

Private members are members that can be shared within the internal implementation of a class and are not exposed externally. There is no special mechanism in JavaScript to define private members, but there are some tricks you can use to implement this functionality.

This technique is mainly achieved through the scope nature of variables, in JavaScript, a function defined within the variable called a local variable, the variable can not be accessed by programs outside this function, but can be accessed by nested functions defined within the function. It is in the process of implementing private members that this nature is exploited.

As mentioned earlier, you can add members to a class in the constructor of a class, and the class members defined in this way actually share the local variables defined inside the constructor, which can be considered private members of the class, for example:


In this way, the private property pp and private method PM are implemented. After running Class1, although it looks like PP and PM These local variables should disappear, in fact because the Class1 is running through new, the objects it belongs to are not gone, so you can still manipulate them by exposing members.

Note: These local variables (private members) are shared by all the public methods defined in the constructor, and are only shared by the public methods defined in the constructor. This means that the class members defined in prototype will not be able to access the local variables (private members) that are defined in the construct body.

The use of private members is at the expense of the readability of the code. And this implementation is more of a javascript technique because it is not a mechanism for the language itself. But this technique, which utilizes the nature of variable scope, is worth learning from.

implementing static Members

A static member belongs to a member of a class and can be accessed by means of a class name. static member name. In JavaScript, you can add members directly to a function object to implement a static member, because the function is also an object, so the associated operation of the object is also applicable to the function. For example:

function Class1 () {//constructor
}
Static properties
class1.staticproperty= "Sample";
static method
Class1.staticmethod=function () {
alert (Class1.staticproperty);
}
Calling static methods
Class1.staticmethod ();
The above code adds a static property and static method to the class Class1, and references the static properties of the class in the static method.

If you want to add a common static method to each function object, you can also do so by using the class function of the functions object, for example:

To add a prototype method to a class function: Show Argscount
Function.prototype.showargscount=function () {
alert (this.length); Displays the number of formal parameters defined by a function
}
function Class1 (a) {
Define a class
}
Call the static method of the class defined by the prototype of the function Showargscount
Class1. Showargscount ();
Thus, the prototype prototype object of function can add universal static members to any functions, which can play a great role in actual development, for example, in the famous Prototype-1.3.1.js framework, the following two methods are defined for all functions:

To run a function as a method of an object
Function.prototype.bind = function (object) {
var __method = this;
return function () {
__method.apply (object, arguments);
}
}
To use a function as an event listener
Function.prototype.bindAsEventListener = function (object) {
var __method = this;
return function (event) {
__method.call (object, Event | | window.event);
}
}
These two methods play a great role in the prototype-1.3.1 framework, and the specific meaning and usage will be described in later chapters.

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.