JavaScript plug-in development tutorial (II) _ javascript skills

Source: Internet
Author: User
The first article in this series discusses how jQuery develops plug-ins. in this article, we will continue to develop plug-ins. I hope you will like it. I. opening analysis

Hi everyone! Remember the previous article ------ the beginning of this series (JavaScript plug-in development tutorial 1 ). This article mainly describes how to develop plug-ins in jQuery mode ",

So today we will continue our plug-in development journey with yesterday's questions. The previous problems are as follows:

(1) if the project technical model changes to these plug-ins and they are strongly dependent on the "jQuery" mechanism, the plug-ins we previously wrote will not be available (assuming jQuery is not used ), how to refactor it?

(2) How do we organize the key logic of refactoring plug-ins?

Now, let's take a look at today's articles with questions.

First, I am not denying the "jQuery plug-in method". Second, we need to analyze the problem from different perspectives. for example, "jQuery plug-in has the following advantages ":

(1) Place all the code in the closure (an immediate execution function). at this time, the closure is equivalent to a private scope, and internal information cannot be accessed from the outside, and there will be no contamination of global variables.

(2) a) avoid global dependencies; B) avoid third-party damages; c) compatible with jQuery operators '$' and 'jQuery '.

Then, in what way will we organize code? is it an object-oriented idea (OOP? Or is it just a procedural approach? Or are they designed in combination? Haha, continue watching ......

2. reconstruct the example of yesterday

The following is part of the source code of yesterday's Js:

The code is as follows:


(Function ($ ){
$. Fn. bigbear = function (opts ){
Opts = $. extend ({}, $. fn. bigbear. defaults, opts );
Return this. each (function (){
Var elem = $ (this );
Elem. find ("span"). text (opts ["title"]);
$. Get (opts ["url"], function (data ){
Elem. find ("p"). text (data ["text"]);
});
});
};
$. Fn. bigbear. defaults = {
Title: "This is a simple test ",
Url: "data. json"
};
}) (JQuery );

Let's perform a line-by-line analysis:

First, determine the functions of this plug-in.

(1) display the title text information we set.

(2) dynamically obtain content information in asynchronous mode.

Okay! The requirement is clear. from the above code, it is not difficult to see that the logic organization is very loose and the procedural thinking is very obvious. so the first step is to put our functional requirements

It is effectively organized as a class. See the code after reconstruction as follows:

The code is as follows:


$ (Function (){
$ ("# Bb"). bigbear ();
});
(Function ($ ){
$. Fn. bigbear = function (opts ){
Opts = $. extend ({}, $. fn. bigbear. defaults, opts );
Return this. each (function (){
Var elem = $ (this );
Var bb = new BigBear (elem, opts );
Bb. getElem (). trigger ("data ");
});
};
$. Fn. bigbear. defaults = {
Title: "This is a simple test ",
Url: "data. json"
};
}) (JQuery );
Function BigBear (elem, opts ){
This. elem = elem;
This. opts = opts;
This. init ();
};
Var bbProto = BigBear. prototype;
BbProto. getElem = function (){
Return this. elem;
};
BbProto. getOpts = function (){
Return this. opts;
};
BbProto. init = function (){
Var that = this;
This. getElem (). on ("data", function (){
That. _ setTitle (that. getOpts () ["title"]);
$. Get (that. getOpts () ["url"], function (result ){
That. getElem (). find ("p"). text (result ["text"]);
});
});
};
BbProto. _ setTitle = function (text ){
This. getElem (). find ("span"). text (text );
};

Hahaha, is there a lot more code? in fact, this method is to look at the problem from the object-oriented perspective. first, analyze the functional requirements and then design our classes. although it is impossible for us to design well,

However, our goal is to make the code readable and maintain it better.

The following is the source code implementation from the "Bootstrap" Js section, for example:

It is not hard to see that it is also a similar implementation method. We use classes to maintain the main logic of our plug-ins.

(3) add new features and lead to additional classes

Now the demand has increased, and the experience needs to be changed. when loading data, there is a "loading" effect.

The implementation idea is as follows. in the original content area, set the text to "loading data ...." And then introduce a new class, as shown below:

The code is as follows:


Function Overlay (){

};
Var olProto = Overlay. prototype;
OlProto. show = function (){};
OlProto. hide = function (){};
// The specific implementation will not be written

Okay, the mask layer already exists. how can we integrate it now? We use a combination of the following methods:

The code is as follows:


Function BigBear (elem, opts ){
This. elem = elem;
This. opts = opts;
This. overlay = new Overlay ();
This. init ();
};
Var bbProto = BigBear. prototype;
BbProto. getElem = function (){
Return this. elem;
};
BbProto. getOpts = function (){
Return this. opts;
};
BbProto. init = function (){
Var that = this;
Var loadingText = "loading data... ";
This. getElem (). on ("data", function (){
That. _ setTitle (that. getOpts () ["title"]);
That. overlay. show ();
That. getElem (). find ("p"). text (loadingText );
$. Get (that. getOpts () ["url"], function (result ){
That. overlay. hide ();
That. getElem (). find ("p"). text (result ["text"]);
});
});
};
BbProto. _ setTitle = function (text ){
This. getElem (). find ("span"). text (text );
};

This is the end of our function. I believe the plug-in written in this way is much better than the first version. of course, this is not the optimal implementation and needs to be reconstructed from the details, however, this method is an optional plug-in development method.

The complete code is as follows:

The code is as follows:


$ (Function (){
$ ("# Bb"). bigbear ();
});
(Function ($ ){
$. Fn. bigbear = function (opts ){
Opts = $. extend ({}, $. fn. bigbear. defaults, opts );
Return this. each (function (){
Var elem = $ (this );
Var bb = new BigBear (elem, opts );
Bb. getElem (). trigger ("data ");
});
};
$. Fn. bigbear. defaults = {
Title: "This is a simple test ",
Url: "data. json"
};
}) (JQuery );
Function BigBear (elem, opts ){
This. elem = elem;
This. opts = opts;
This. overlay = new Overlay ();
This. init ();
};
Var bbProto = BigBear. prototype;
BbProto. getElem = function (){
Return this. elem;
};
BbProto. getOpts = function (){
Return this. opts;
};
BbProto. init = function (){
Var that = this;
Var loadingText = "loading data... ";
This. getElem (). on ("data", function (){
That. _ setTitle (that. getOpts () ["title"]);
That. overlay. show ();
That. getElem (). find ("p"). text (loadingText );
$. Get (that. getOpts () ["url"], function (result ){
That. overlay. hide ();
That. getElem (). find ("p"). text (result ["text"]);
});
});
};
BbProto. _ setTitle = function (text ){
This. getElem (). find ("span"). text (text );
};
Function Overlay (){
};
Var olProto = Overlay. prototype;
OlProto. show = function (){};
OlProto. hide = function (){};
// The specific implementation will not be written

This article has come here for the time being. do you have a new understanding of plug-in-based javascript development.

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.