Pending Notes (Form serialization operations, polymorphic functions, State queue management mode for objects)

Source: Internet
Author: User

1.jquery Form serialization operation

$ ('. Form '). Serializearray (); the Serializearray () method is used to obtain an array of value values for all nodes containing the name value under the DOM node of form form (class= "form");

This array consists of multiple objects, each with a name and value of two key values.

2. Programming polymorphism thought

By using the function's parameter array arguments, the function can be executed selectively by judging the length of the array.

For example:

Store:function (namespace, data, remove) {

Store content
Namespace: namespaces
Data: Stored Object
Using polymorphism to process
if (Arguments.length > 2) {
Localstorage.removeitem (namespace);
}else if (Arguments.length > 1) {
Localstorage.setitem (namespace, json.stringify (data));
}else {
var strobj = Localstorage.getitem (namespace);
Return (Strobj && json.parse (strobj)) | | {};
}
}

As shown in the previous function, when the parameter array is longer than 2 o'clock, perform the remove operation

Performs a storage operation when the parameter array is longer than 1 o'clock

When the parameter array length is 1 o'clock, the storage operation is performed;

3. Programming mode for State queue management; (object)

For example there are five objects, which are made up of their object names by the array var targetlist = [' Home ', ' Rank page ', ' form page ', ' City List page ']. We can intuitively understand the object structure and execution order of the project through the targetmap Array (sorted by the index value of the array);

Then by var targetmap = {

' Home ': Homemodule,

' Rank page ': Rankmodule,

' Form page ': Formmodule,

' City list page ': Citylistmodule

}

Object table to navigate to the execution object;

Now let's implement the jump for each module and feel how elegant the code is in this mode.

  Targetlist.current = null;

Targetlist.index =-1;

Targetlist.next = function () {

    targetlist.index++;

Targetlist.current && targeList.current.leave && targetList.current.leave ();//First step to implement the Leave method of the current operation module

var key = targetlist[targetlist.index];//Second step to find the module that will be entered and execute its enter () method

var module = Targetmap[key];

Module && module.enter ();

Targetlist.current = module; Finally let the current operation module = Incoming module assignment ;

}

Targetmap[targetlist[targetlist.length-1]].leave = function () {};//The Leave method of the object that is the lowest of the overloaded state sequence;

Targetlist.next ();//display the first module;

This invokes the next () method when an object's binding event occurs, such as a click event, and each object does not need to know where to go next, and the jump target is managed by TargetList and Targetmap.

Graceful to achieve the goal of the jump;

Pending Notes (Form serialization operations, polymorphic functions, State queue management mode for objects)

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.