Decorator mode [add to favorites]

Source: Internet
Author: User

I don't know if the "decorator mode" is suitable here. JS is not an object-oriented language, it is also a weak language type, and the design mode is object-oriented language. The two seem to be far different.

But it is because JS is a weak language type that leads to this "decorator mode ". In fact, as a mode, the decorator or other models do not need to have a fixed UML diagram, or must be object-oriented. It is just an idea. I understand the following decorator:

Add some additional responsibilities to an object

 

If we regard some functions in JS as "objects", let's take a look at how to "Decorate" them in Js ".

I. Using JS "overload" Method

Suddenly I found that JS has a very good mechanism (in fact, a JS expert gave me some advice, ). It can "reload" some functions, with this feature, we can dynamically "reload ". Refer to the following code:

Window. Alert =;

Function ()

{

Window. Location = 'HTTP: // localhost/a. aspx ';

}

On this page

What happens to alert ('abc? The page does not display a prompt box, but changes to the above URL. That is to say, when running alert, function a is executed, not the default function of the system.

 

Ii. Add new responsibilities for old functions using JS "overload"

We can dynamically add new functions based on the old functions, while maintaining the original function signature. I think this is already a decorator. refer to the following code:

What do you get when you run it? After the message is displayed, the page jumps to http: // localhost/Quickstart/

<Body>

<Script language = "JavaScript">

<! --

VaR temp = Window. Alert;

Window. Alert =;

Function A (alertvalue)

{

Temp (alertvalue );

Window. Location = 'HTTP: // localhost/Quickstart /';

}

 

Alert ('Microsoft quick start tutorial ');

// -->

</SCRIPT>

</Body>

Process:

Declare a temp variable to save the original alert function of the system;

Assign a new method (function pointer? ^_^), The new method means the new responsibilities.

In the new method, call the original system function saved in temp.

 

Iii. Practical Application

A project has many pages using Windows. open method, and various implementation methods, which are directly written in JS, response in codebehind, and also used.. Net registers the script block, and the parameters are different. Suddenly there is a need to change in the project, and all the popup is required to be centered. If the window. Open method is found one by one and then modified at this time, it is estimated that it will be crazy.

In this case, we can rewrite the window in the base class (commonpage) of all pages. open, add the "center" responsibility for it and keep the original function signature, that is, the three parameters remain unchanged, so that all open projects are centered.

The reference code is as follows:

'By dingsea 11-21
'For adjusting the pop window to center.
Private function registerscripttoadjustpopwindow () as string
Dim spop as stringbuilder = new stringbuilder ("<script language =" + CHR (34) + "JavaScript" + CHR (34) + ">" + CHR (13 ))
Spop. append ("Var o = Window. Open;" + CHR (13 ))
Spop. append ("window. Open = launchcenter;" + CHR (13 ))
Spop. append ("function launchcenter (URL, name, features)" + CHR (13 ))
Spop. append ("{" + CHR (13 ))
Spop. append ("Var width = features. substring (features. indexof ('width = ') + 6);" + CHR (13 ))
Spop. append ("width = width. substring (0, width. indexof (',');" + CHR (13 ))
Spop. append ("Var Height = features. substring (features. indexof ('height = ') + 7);" + CHR (13 ))
Spop. append ("Height = height. substring (0, height. indexof (',');" + CHR (13 ))
Spop. append ("features + = ', innerheight =' + height;" + CHR (13 ))
Spop. append ("features + = ', innerwidth =' + width;" + CHR (13 ))
Spop. append ("If (window. Screen) {" + CHR (13 ))
Spop. append ("features + = ', Top =' + (screen. availheight-height) * 0.5 + ', Left =' + (screen. availwidth-width) * 0.5; "+ CHR (13 ))
Spop. append ("}" + CHR (13 ))
Spop. append ("O (URL, name, features);" + CHR (13 ))
Spop. append ("}" + CHR (13 ))
Spop. append ("</SCRIPT> ")

Return spop. tostring
End Function

Use the above Code when loading a page:

Private sub page_prerender (byval sender as system. Object, byval e as system. eventargs) handles mybase. prerender
Response. Write (registerscripttoadjustpopwindow () 'By dingsea
End sub

Why not use the built-in. Net Registration script block? I want this rewrite code to be at the top of the page. If a script block is registered, it will appear in the body or somewhere else, if I directly respond to page render, it will be sent to the client before HTML and all JS, so as to ensure any window on the page. open Can be centered.

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.