A design pattern for JavaScript

Source: Internet
Author: User
Tags define object definition functions implement return window
Design

Simple design Patterns for classes
Define a class Class1
function Class1 () {
Constructors
}

To implement the member definition of a class by specifying a prototype object
Class1.prototype = {
Someproperty: "Simple",
somemethod:function {
Method code
},
In fact, properties and methods
The members of a class are referenced to each other, and must be done through the this pointer. Because the first property and method are independent in JavaScript, they are contacted by the this pointer on an object.

Simple event design pattern with parameters
<script language= "JavaScript" type= "Text/javascript" >
<!--
To encapsulate a function with parameters as a function without parameters
function createfunction (obj, Strfunc) {
var args = [];//definition args used to store parameters passed to an event handler
if (!obj) obj = window;//If it is a global function Obj=window;
Get the arguments passed to the event handler
for (var i=2; i<arguments.length; i++) {
Args.push (Arguments[i]);
}
To encapsulate the invocation of an event handler with a parameter-free function
return function () {
Obj[strfunc].apply (obj, args);//pass parameters to the specified event handler
}
}

Define class Class1
function Class1 () {
Constructors
}
Class.prototype = {
Show:function () {
Implementation of Show function
This.onshow ()///Trigger OnShow Event
},
Onshow:function () {}//defines the event interface
}
To create an instance of Class1
var obj = new Class1 ();
Create an OnShow event handler for obj
function Objonshow (userName) {
Alert ("Hello," +username);
}
Define variable username
var userName = "Terry";
OnShow events bound to obj
Obj.onshow=createfunction (NULL, "Objonshow", userName);
Call Obj's Show method
Obj.show ();
-->
</script>
Through Createfunction encapsulation, we can implement parameter transfer with a general scheme.
A simple development framework
<script language= "JavaScript" >
var http_request = false;
function send_request (URL) {//initialization, specifying handler functions, sending requests
Http_request = false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest) {//mozilla browser
Http_request = new XMLHttpRequest ();
if (http_request.overridemimetype) {//Set MIME category
Http_request.overridemimetype ("Text/xml");
}
}
else if (window. ActiveXObject) {//IE browser
try {
Http_request = new ActiveXObject ("Msxml2.xmlhttp");
catch (e) {
try {
Http_request = new ActiveXObject ("Microsoft.XMLHTTP");
catch (e) {}
}
}
if (!http_request) {//exception, failed to create object instance
Window.alert ("Cannot create XMLHttpRequest object instance.");
return false;
}
Http_request.onreadystatechange = ProcessRequest;
Determine how and when the request is sent and whether the next code is executed synchronously
Http_request.open ("Get", url, True);
Http_request.send (NULL);
}
Functions that process return information
function ProcessRequest () {
if (http_request.readystate = = 4) {//Judge object state
if (Http_request.status = = 200) {//information has been successfully returned to start processing information
alert (Http_request.responsetext);
} else {//page is not normal
Alert ("The page you are requesting has an exception.) ");
}
}
}
</script>
Http://www.cnblogs.com/skylaugh/archive/2006/11/20/566182.html



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.