JS Difficulty--component development

Source: Internet
Author: User

JS Component

encapsulates an effect or function with an object-oriented approach, providing only to the user some relevant methods or data interfaces

1. features

Easy to expand, easy to maintain. There's no effect on each other .

 

2. Composition of Components

① Configuration Parameters

in the initialization function , This is a paragraph about drag-and-drop code

initialization function

Drag.prototype. init=function(opt) {    this. settings={        downFn:opt.downFn| | function () {},        moveFn:opt.moveFn| | function () {},        upFn:opt.upFn| | function (){}    };  };        

Default parameters

// This is the default parameter, the following program will have to use the parameters here this. settings={    ID:",        // This property is a must-pass    downfn:function () {},        Movefn:function() {},    UPFN:function() {}};

To take a user-passed parameter, go to override the default parameter and use the For in method

//initialization functiondrag.prototype.init=function(opt) {//in the initialization function, take the user passed in parameters to override the default parameters, and this code is placed on the top of the init function     for(varattrinchopt) {        if( This. Settings.hasownproperty (attr)) {        //If this attribute is in the default parameter, then the user-passed parameter is overwritten             This. settings[attr]=Opt[attr]; }    }                     This. Obj=document.getelementbyid ( This. settings.id); varthis= This;  This. obj.onmousedown=function(EV) {this.down (EV); };};

② Method

functions placed in the prototype

Drag. prototype.down=function(EV) { This. Settings.downFn.call ( This);  This. disx=ev.clientx- This. Obj.offsetleft;  This. disy=ev.clienty- This. Obj.offsettop; varthis= This; Document.onmousemove=function(EV) {this.move (EV);    }; Document.onmouseup=function() {this.up ();                                };}; 

③ Custom Events

except for events outside the system

    Event Binders , event triggers required

Events that are defined by themselves, in addition to system-provided events. facilitates multi-person collaborative development, can expand JS Original event

A custom event is actually called a function that triggers the function in the specified environment, which is equivalent to an event.

(1) Custom events three elements

Object, event name, event handler function

1, can use the object data structure in the form of Key:value to the event name and event processing function corresponding

2, put a number of functions in an array, loop to execute each function in the array, you can do all the functions to execute

 

(2) Defining Events

obj.events={

' event name 1 ': [fn1,fn2],

' event name 2 ': [Fn3,fn4,fn5],

}

 

// Adding custom events function customevent (OBJ,EVENTNAME,FN) {    obj.events=obj.events| | {};        // If you already have this attribute on obj, use your own    obj.events[eventname]=obj.events[eventname]| | [];        // If there is content in events, use your own    Obj.events[eventname].push (FN);        // add an event to the array by using the Push method }    

(3) Invoke event (loop to tune)

obj.events[' event name 1 '].fn1 ();

obj.events[' event name 1 '].fn2 ();

// Triggering custom Events function Trigger (obj,eventname) {    // trigger to see if the object has this event, event is placed on the object of events under the    if  (Obj.events[eventname]) {        for (var i=0;i<obj.events[eventname].length ; i++) {            obj.events[eventname][i].call (obj);     }};

Invoke Event

Customevent (box, ' click ',function() {    console.log (' box clicked ');});

 

JS Difficulty--component 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.