Javascript object-oriented namespace _js object-oriented

Source: Internet
Author: User
There is no concept of namespaces in JavaScript, but to embody object-oriented thinking,
There should be a namespace, just like the namespace in Package,.net in Java,
The main function is to prevent class name conflicts, the same class name as long as the different namespaces, will not conflict.
The easiest way to create namespaces:
Copy Code code as follows:

var java = {};
Java.util = {};
This creates a successful namespace: 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 results: Java.util.HashMap
Encapsulates how namespaces are created:
Define an object, JS uses {} curly braces to define objects, equivalent to var jsobject = new Object ();
var jsobject = {};
Jsobject.namespace = function ()////////////////////Jsobject
{
/* The following code arguments the arguments passed into the function, and when the function does not explicitly define the parameter,
function can also pass in parameters and receive them with arguments, arguments like arrays,
If more than one argument is passed in, the value is saved in order: arguments[0],arguments[1]....*/
var a = Arguments,o = Null,d,rt;
for (var i = 0; i < a.length; i++)
{
D = a[i].split ('. '); The incoming arguments are segmented with the symbol '. ' and put into the D array.
RT = D[0];
Determines whether the first value in the array is undefined and, if not defined, is defined as an empty object {} and assigned to the variable O
Eval (' if (typeof ' + rt + ' = ' = ' undefined ') {'
+ rt + ' = {};} o = ' + rt + ';
for (var j = 1; j < D.length; J + +)
{
/* Loop traversal array d each value as key, add to Object o, if the key exists in O, then take the o median, if
Does not exist, the assignment is an empty object {} * *
O[D[J]] = o[d[j]] | | {};
o = O[d[j]];
}
}
}
Jsobject.namespace ("Org.myjs"); DECLARE namespaces: ORG.MYJS
Org.myJs.Student = function ()//-Define class under namespace Org.myjs Student
{
Defines a variable in the class student and assigns an initial value, but the access rights for this variable are public
This.studentno = ' s001 ';
This.studentname = ' xiaoming ';
This.sex = ' Male ';
}
var s = new org.myJs.Student (); To create an object of the student class
Alert (' School No.: ' +s.studentno);
Alert (' Name: ' +s.studentname);
Alert (' Sex: ' +s.sex);

Effects and first (i) JavaScript experience summary object-oriented-class results like
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.