JavaScript object-oriented part 2 namespace_js object-oriented
The simplest way to create a namespace:
var java = { };
java.util = {};
//This creates the namespace successfully: 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
// Encapsulate the method of creating a namespace:
//Define an object. Use {} braces to define the object in js, which is equivalent to var JsObject = new Object();
var JsObject = {};
JsObject.namespace = function() //Define a function namespace under the JsObject object
{
/*In the following code, arguments are the parameters passed in by the function. When the function does not clearly define parameters,
function can also pass in parameters and receive them with arguments. Arguments are similar to arrays,
If multiple parameters are passed in, they will be saved in order. The value method is: arguments[0], arguments[1]....*/
var a = arguments,o = null,d,rt;
for(var i = 0; i < a.length; i )
{
d = a[i].split('.'); //Use the incoming parameters with the symbol '.' Split and put into d array.
rt = d[0];
//Determine whether the first value in the array is undefined. If it is undefined, define it as an empty object {} and assign it to the variable o
eval(' if (typeof ' rt ' == "undefined"){'
rt ' = {};} o = ' rt ';');
for(var j = 1; j < d.length; j )
{
/*Loop through each value of array d as a key and add it to object o. If key exists in o, take the middle value of o. If
does not exist, assign the value as 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 under the namespace org.myJs Student
{
//Define a variable in class Student and assign it an initial value, but the access permission of this variable is public
this.studentNo = 's001';
this.studentName = 'Xiao Ming ';
this.sex = 'Male';
}
var s = new org.myJs.Student(); //Create an object of Student class
alert('Student number: ' s .studentNo);
alert('Name:' s.studentName);
alert('Gender:' s.sex);
Effects and the first article (1) javascript Experience summary object-oriented - class results are the same

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solve PHP error: The specified namespace class was not found. When developing using PHP, we often encounter various error messages. One of the common errors is "The specified namespace class was not found". This error is usually caused by the imported class file not being properly namespace referenced. This article explains how to solve this problem and provides some code examples. First, let’s take a look at an example of a common error message: Fatalerror:UncaughtError:C

The F3 framework is a simple, easy-to-use, flexible and scalable PHPWeb framework. Its namespace (Namespace) mechanism provides us with a more standardized, more readable, and clearer code structure. In this article, we will explore how to use namespaces in the F3 framework. 1. What is a namespace? Namespaces are often used to solve the problem of naming conflicts in PHP. It can encapsulate one or more classes, functions or constants in a namespace, which is equivalent to adding a prefix to them. example

Redis is an open source, high-performance key-value storage database. When using Redis for data storage, we need to consider the design of the key namespace and expiration mechanism to maintain Redis performance and data integrity. This article will introduce the design ideas and implementation methods of Redis' namespace and expiration mechanism. 1. Redis namespace design ideas In Redis, keys can be set arbitrarily. In order to facilitate the management and distinction of different data types, Redis introduces the concept of namespace. Life

C++ is a widely used high-level programming language. It has high flexibility and scalability, but it also requires developers to strictly master its grammatical rules to avoid errors. One of the common errors is "use of undefined namespace". This article explains what this error means, why it occurs, and how to fix it. 1. What is the use of undefined namespace? In C++, namespaces are a way of organizing reusable code in order to keep it modular and readable. You can use namespaces to make functions with the same name

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Introduction: PHP8 is an important version of the PHP programming language, which introduces many exciting new features and improvements. One of the most important new features is namespaces. Namespaces are a way to organize your code into a better structure that avoids conflicts between classes, functions, and constants with the same name. In this article, we’ll look at how to leverage namespaces and codes to better structure your PHP8 code

New features of PHP5.3: How to use namespaces to solve class name conflicts Introduction: During the development of PHP, as projects become larger and more complex, class name conflicts also arise. In order to solve this problem, PHP5.3 version introduced the concept of namespace. Namespaces provide a way to organize related classes, functions, and constants together to avoid naming conflicts. This article will introduce in detail the concept of PHP namespaces and how to use namespaces to solve class name conflicts, with code examples.

PHP Autoloading Overview Autoloading is a mechanism for automatically loading classes and their dependencies before use. In PHP, this is achieved by using the __autoload() function or an autoloader such as Composer. Proper autoloading settings are critical to ensuring the stability and performance of your codebase. PSR-4 automatic loading standard PSR-4 is the automatic loading standard defined by PHP-FIG. It is based on namespace and directory structure conventions to simplify class file lookup. To comply with PSR-4: define a root namespace (e.g. MyApp). Use backslash() as namespace separator. Use lowercase letters to represent namespace elements. Create a corresponding directory for each namespace element. General essay

How to resolve PHP namespace errors and generate corresponding error messages. PHP is a widely used server-side scripting language that is used to develop web applications. In PHP, namespace (Namespace) is a mechanism for managing and organizing code, which can avoid naming conflicts and improve code readability and maintainability. However, the complexity of namespace definition and use sometimes leads to errors. This article will introduce some methods to solve PHP namespace errors and generate corresponding error prompts. 1. Name space
