Self-encapsulated JS components-Beginner

Source: Internet
Author: User

2 days ago with the attitude of trying to register this god Bo, a whim to write an article about the Vue custom components of the small article a comment on the No! There is no popularity! (Of course, my article is limited in content and the gods of the comparison is simply shoddy ...) I just turn this blog into a record of my own study. Maybe this blog is the original intention of the same! But I this is a net red heart ah ...

Words do not say more into the topic, before you see all kinds of great God himself with JS encapsulation plug-ins and then all over their own use of real to say that I have a few years of the page learning ah how to write a component is not it! Just then I finally found a website in a very suitable for beginners to start their own writing plug-in tutorial feel benefited a lot today I will be a knowledge of the porter to share the contents of the class as well as their own future review notes it!

First of all, please understand that the use of require.js this case using require.js concatenation is not mentioned here (because it is a learning record so it is not expanded here but if you understand require.js follow-up content should not be a problem) also in this lesson the teacher wrote this component is also gradual Continuous improvement of my notes will be a step by step record to prevent my pig brain after a few days to see and forget (I often myself yesterday did nothing to forget that no way to get old, so please don't laugh at me)

Objective: To develop an alert popup module with JS package

define ([' jquery '],function ($)} {

Define the window class to give some basic parameter configuration

function Window () {

This.cfg = {
width:400,
HEIGHT:200,
Content: ' I am the default text content ',
}

}

Add a method to the window class

Window.prototype = {

var CFG = $.extend (this.cfg,cfg);

Alert:function (CFG) {

var BoundingBox = $ (' <div class= ' window_boundingbox ' ></div> ');

Boundingbox.appendto (' body ')

BoundingBox. HTML (CFG content);

Boundingbox.css ({

Width:cfg. Width,

Height:CFG.height,

Left: (cfg.x | | (window.innerwidth-cfg.width)/2) + ' px ',

Top: (Cfg.y | | (window.innerheight-cfg.height)/2) + ' px ',

})

}

}

}

Export Window class

return {

Window:window

}

})

The following is called in Main.js

Require ([' jquery ', ' Window '],function ($,w) {
New W.window (). Alert ({
WIDTH:500,
HEIGHT:300,
Content: ' Happy New Year ',
})
})

This is the use of JS self-encapsulation plug-in phase B stage but in the master looks still very low because this is not finished is the pop-up frame you told me this is the alert popup component??? No, it's okay. Since there is a beginner's B guide, there will be intermediate or even advanced to be continued ...

Self-encapsulated JS components-Beginner

Related Article

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.