JS Object-oriented code

Source: Internet
Author: User

Put on a section of colleagues write code, value of the memorial

<script type= "Text/javascript" >//Create a box class    functionBox (option) {varSelf = This; var_option ={height:12, Width:12        }; //Merging Parameter objects: Remember to quote Jquery.js$.extend (_option, option);  This. Height =_option.height;  This. width =_option.height; //Binding Event List        var_events = {}; //Create a method with a callback function         This. Push =function(option, callback) {if(Option.height > This. Height) {Console.error ("Over height", This); return; }            if(Option.width > This. Width) {Console.error ("Over width", This); return; }            //determine if there is a callback function            if(CallbackinstanceofFunction)//call the callback function and give it a value (parameter: _option)Callback.call ( This, _option); }        //Binding Events         This. on =function(name, event) {if(Name = =NULL|| !name)return NULL; if(! (EventinstanceofFunction)) if(Self[name]instanceofFunction) {                    returnSelf[name] (); }            if(EventinstanceofFunction) _events[name]= Self[name] =function() {event.apply ( This); return  This; }        }        //Unbind Event         This. Unbind =function(name) {DeleteSelf[name]; Delete_events[name]; //Chained Expressions            return  This; }    }    //Create a Box1 object    varBox1 =NewBox (); //Create a Box2 object    varBox2 =NewBox (); //Calling MethodsBox1.push ({height:12, Width:12//callback function},function(option) {Console.log ("The push callback function has been called:", option);    }); //dynamically binding events to ObjectsBox1.on ("click",function() {         This. width + = 10; Console.log (The "width increment 10,click event is called ...", This. width);    }); Box1.on ("Heihei",function() {Console.log ("The Heihei event was called ...", This. width);    }); //Event Normal CallBox1.click (); //chained expression invocation methodBox1.on ("click"). Click (). Heihei (). On ("Heihei"); //Print the width of the box1Console.log (box1.width); //Print the width of the Box2Console.log (box2.width); //Unbind EventBox1.unbind ("Heihei"); //Test ReleaseBox1.heihei ();</script>

JS Object-oriented code

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.