ATLAS learning note (27): javascript object-oriented extension (1): namespace

Source: Internet
Author: User

InJavascriptThere are no space, classes, interfaces,AtlasThese things are encapsulated and enhancedJavascriptThis article describes how to use namespaces for Object-Oriented capabilities.

 

Main Content

1. Overview

2. Complete example

 

I. Overview

InJavascriptThere are no space, classes, interfaces,AtlasThese things are encapsulated and enhancedJavascriptThis article describes how to use namespaces for Object-Oriented capabilities. Note the following two methods when using a namespace.

Registernamespace: Register a namespace

Registerclass: Register a class to a namespace.

Ii. Complete example

1. CreateAtlas web siteAddNamespace. jsHere we registerDemoAnd createPersonClass, register itDemoIn the namespace, as follows:

// JScript File

Type. registernamespace ( " Demo " );

Demo. Person =   Function (Firstname, lastname, emailaddress) {

VaR _ Firstname = Firstname;

VaR _ Lastname = Lastname;

VaR _ Emailaddress = Emailaddress;

This . Getfirstname =   Function () {

Return_ Firstname;

}

This . Getlastname =   Function () {

Return_ Lastname;

}

This . Getname =   Function () {

Return_ Firstname+''+_ Lastname;

}

This . Dispose =   Function () {

Alert ('bye'+ This. Getname ());

}

}

Demo. Person. registerclass ('demo. person ', Null , SYS. idisposable );

2InAspxAddScriptmanager, This is always indispensable:

< Atlas: scriptmanager Runat = "Server" ID = "Scriptmanager"   />

3. IntroduceJSFile:

< Script Type = "Text/JavaScript" SRC = "Namespace. js" > </ Script >

4Now you can call it in the front-end script:

< Script Type = " Text/JavaScript " Language = " Javascript " >

Function Onbutton1click ()

{

VaR Testperson =   New Demo. Person ('john', 'Smith ', 'John. smith@example.com ');

Alert (testperson. getfirstname () +   "   "   + Testperson. getlastname ());

Return   False ;

}

</ Script >

5. AddButton, Which is called in its click event:

< Div >

This example and puts the person class in the "Demo" namespace.

< BR />

< BR />

< Input ID = "Button1" Value = "Create demo. Person" Type = "Button" Onclick = "Return onbutton1click ()"   />

</ Div >

Let's take a look atCodeIs it very object-oriented? Compile and run:


Click the button:

Full sample download: http://files.cnblogs.com/Terrylee/AtlasNamespaceDemo.rar

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.