JavaScript components: Coding implementations and algorithms

Source: Internet
Author: User
Tags array version throw exception

In the last issue we discussed the design of the queue management component and gave it a loud and unique name: Smart queue. This time, we want to put the previous design results into practice, using code to implement it.

First, we need to consider the layout of its source files, which is how the code splits into separate files. Why would you do that? Remember when I mentioned at the end of the last period that this component would use "External code"? To differentiate the use of code, decide to divide the code into at least two parts: an external code file and a Smart Queue file.

Distinguishing between uses is only one, and the other is that it is advantageous for code maintenance to spread to separate files. Just imagine that someday you decide to have the basic functionality of the existing queue management, adding new extensions, or wrapping it up as a component that implements a specific task, and wanting to keep the existing functionality (internal implementation) and invocation (external interface) unchanged, then writing the new code to a separate file is the best choice.

Well, the next issue will focus on the topic of file layout, and now it's time to start cutting to the chase. The first step, of course, is to create your own namespace for the component, and all the code for the component will be confined to the top-level namespace:

var smartqueue = window. Smartqueue {};
Smartqueue.version = ' 0.1 ';

When initialized, pull it over if you encounter a namespace conflict. Typically, this conflict is caused by repeated referencing of component code, so "pull over" will rewrite the object one time with the same implementation; at worst, if it happens that another object on the page is called Smartqueue, I'm sorry, I'll overwrite your implementation--if there's no further naming conflict, Basically two components can run peacefully. At the same time give it a version number.

Next, create three queues for Smartqueue by three priorities:

var Q = Smartqueue.queue = [[], [], []];

Each is an empty array because there is no task to add to it. Also by the way to build a "shortcut", to access the array to write directly q[n] on it.

Next, our protagonist task is a grand debut-how to new a task, defined here:

Inside the details do not say, there are necessary comments, the general our code can also do self-describing, the following code is the same. Here to tell the customer (user): You want to create a Smartqueue.task instance, you need to pass at least one parameter to the constructor (the last 3 can be omitted for default processing), otherwise throw exception to wait.

But that's not enough. Sometimes a customer wants to clone a new instance from an existing task, or fix a "health body" (a real Task object instance) from a "disabled body" (an object with a partial task attribute), which is a bit uncomfortable by the way it was constructed--the customer has to write:

var task1 = new Smartqueue.task (Obj.fn, 1, ', obj.dependencies);

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.