Javascript object-oriented namespace _ js object-oriented

Source: Internet
Author: User
Javascript does not have the namespace concept, but to embody the object-oriented thinking, there should be a namespace, just like the package in java ,. the namespace in. net is used to prevent class name conflicts. The same class name does not conflict as long as it belongs to different namespaces. The simplest way to create a namespace is:

The Code is as follows:


Var java = {};
Java. util = {};
// The namespace is successfully created: java. util
// We can add classes (functions), attributes, or objects under java. util.
Java. util. HashMap = function ()
{
This. ShowMessage = function ()
{
Alert ("java. util. HashMap ");
}
}
Var map = new java. util. HashMap ();
Alert (map. ShowMessage (); // display result: java. util. HashMap


// Encapsulate the method for creating a namespace:
// Define an Object. The Object is defined in braces {} in js, equivalent to var JsObject = new Object ();

The Code is as follows:


Var JsObject = {};
JsObject. namespace = function () // define a function namespace under the JsObject object
{
/* In the following code, arguments is the parameter passed in by the function. When the function does not explicitly define the parameter,
Function can also input parameters and use arguments to receive them. arguments is similar to an array,
If multiple parameters are input, they are saved in order. Valid values: arguments [0], arguments [1]... */
Var a = arguments, o = null, d, rt;
For (var I = 0; I <a. length; I ++)
{
D = a [I]. split ('.'); // splits the passed parameters with the symbol '.' and puts them in the d array.
Rt = d [0];
// Determine whether the first value in the array is undefined. If not, it is defined as an empty object {} and assigned to variable o
Eval ('If (typeof '+ rt +' = "undefined "){'
+ Rt + '={};} o =' + rt + ';');
For (var j = 1; j <d. length; j ++)
{
/* Cyclically traverse array d. Each value is used as the key and added to object o. If the key exists in o, the o value is used.
If no value exists, the value is assigned to an empty object {}*/
O [d [j] = o [d [j] || {};
O = o [d [j];
}
}
}
JsObject. namespace ("org. myJs"); // declare the namespace: org. myJs
Org. myJs. Student = function () // define the class Student under the namespace org. myJs
{
// Define the variables in the class Student and grant the initial value, but the access permission for this variable is public
This. studentNo = 's001 ';
This. studentName = 'xiaoming ';
This. sex = 'male ';
}
Var s = new org. myJs. Student (); // create a Student Class Object
Alert ('student ID: '+ s. studentNo );
Alert ('name: '+ s. studentName );
Alert ('gender: '+ s. sex );


The effect is the same as that of article 1 (1) javascript Experience Summary object-oriented-class results
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.