Incomplete analysis of JavaScript event drive models

xiaoxiao2021-03-06  14

Since JavaScript is not a "object-oriented" language, it will always encounter some difficulties when implementing an event-driven model.

Of course, the event driver refers to the event handling mechanism of JavaScript, or the event model of the DOM, but is the event model similar to C # or Java.

JavaScript is the biggest problem when processing event drivers is "this pointer" confusion.

For example:

There is no problem with the THIS pointer here.

However,

Var objbtn = document.getlementByid ("Button1");

Objbtn.attachevent ('Onclick', EventHandler);

Function EventHandler ()

{

Alert (this.Value);

}

Running this script is very surprised to find out the result of a complete error

Because this this is not Objbtn this INPUT object! But the global Window object!

Similar questions will appear often in JavaScript

A more complex application:

Function Mainform (Form)

{

THIS.FORM = form;

THIS.BUTTON1 = form.buttonok;

THIS.BUTTON2 = form.buttoncancel;

this.button1.onclick = button1_click; // Register for the button

This.button2.onclick = button2_click;

Mainform.Prototype. Button1_click = function ()

{

Alert (this.button1.value);

}

Mainform.Prototype.Button2_click = function ()

{

This.form.Style.display = 'none';

}

}

Originally "beautiful" code, but can't run correctly, why? Originally, on this, Button1_Clicked and Button2_Clicked two functions are the member functions of the Mainform class, but they are called this.button1.onclick and this.button2.onclick (not fully accurate statement), so in functions This refers to both objects of Button1 and Button2! This is very troublesome, isn't it?

In order to solve this problem, you must ensure that the function caller is the mainform object instead of button or other members. To this end, I think about a mechanism for event registration, write the above code:

THIS.BUTTON1.Clickevent Sheventsender = this

This.button1.onclick = function () {this.clickeventsender.button1_click (this.clickeventsender, self.event);

Mainform.Prototype.Button1_Click = Function (Object, Sender)

{

Alert (this.button1.value);

Get the correct result.

Thus, a complete "event-driven" model based on "background code" is slowly formed, the following is a short DEMO code:

// Background code

// Event Driver Framework (Demo)

// 2004-11-16 Created

// 2004-11-17 Modified

//ControlDemo.js

Function ControlDemo (Page)

{

// Initialize Page

IF (Page == Null)

{

Page = SELF;

}

IF (page! = Self)

{

// do sth. Here ...

}

THIS.PAGE = Page;

// Properties

THIS.KEYPRESSED = 0;

// Controlable Entities

// PageBody Register PageLoad Event

THIS.BODY1 = Page.Document.getlementByid ("main");

this.pageloadaDeventsender = this;

THIS.BODY1.ONLOAD = function () {this.pageload (this.pageloadeventsender, page.event);

// Button1 Register Click event

THIS.BUTTON1 = Page.Button1;

this.button1.value = "OK";

THIS.BUTTON1.Clickevent Sheventsender = this

This.button1.onclick = function () {this.clickeventsender.button1_click (this.clickeventsender, page.event);

// Button2 Register Click event

THIS.BUTTON2 = page.button2;

This.Button2.Value = "Cancel";

THIS.BUTTON2.Clickevent Sheventsender = THIS;

This.button2.onclick = function () {this.clickeventsender.button2_click (this.clickeventsender, page.event);

// TextBox1 Register Keyup, MouseMove Event

THIS.TEXTBOX1 = page.TextBox1;

This.TextBox1.style.Width = "100%";

THIS.TEXTBOX1.ROWS = 10;

THIS.TEXTBOX1.KEYUPSENDER = THIS;

THIS.TEXTBOX1.ONKEYUP = function () {this.keyupsender.textbox1_keyup (this.keyupsender, page.event);

THIS.TEXTBOX1.MOUSEMOVESENDER = THIS;

This.TextBox1.onmousemove = function () {this.mousemovesender.textbox1_mousemove (this.mousemovesender, page.event);

// Labels

THIS.LABEL1 = Page.Document.getlementByid ("label1");

This.Label2 = page.document.getlementByid ("label2"); this.label3 = page.document.getlementByid ("label3");

// EventHandlers event handler

ControlDemo.Prototype.pageLoad = function (sender, event)

{

This.page.defaultstatus = "Event Driver Framework Demo ~~";

THIS.PAGE.ResizETO (600, 400);

}

ControlDemo.Prototype.Button1_Click = Function (Sender, Event)

{

Alert ("Hello ^ _ ^");

}

ControlDemo.Prototype.Button2_Click = Function (Sender, Event)

{

IF (Page.opener == Null)

{

Page.opener = page;

}

Page.Close ();

}

ControlDemo.Prototype.textBox1_keyup = function (sender, event)

{

THIS.KEYPRESSED ;

THIS.Label1.innertext = this.keypressed;

THIS.Label2.innertext = this.TextBox1.Value.LENGTH;

THIS.Label3.innertext = Event.Keycode;

}

ControlDemo.Prototype.textBox1_mousemove = function (sender, event)

{

THIS.PAGE.STATUS = "Mouse Position: x =" Event.x "Y =" Event.y;

}

}

Event Driver Framework Demo </ Title></p> <p></ hEAD></p> <p><body id = "main"></p> <p><div align = "center" id = "DIV1"></p> <p><textarea name = "textbox1" id = "textbox1"> </ textarea> <<</p> <p>The number of keyboard clicks is <span id = "label1"> 0 </ span> Time, type <span id = "label2"> 0 </ span> characters, the last input character code is</p> <p><span id = "label3"> 0 </ span></p> <p><br/></p> <p><Input Type = "Button" name = "button1" id = "button1"> </ input></p> <p><Input Type = "Button" name = "button2" id = "button2"> </ input></p> <p></ div></p> <p></ body></p> <p></ html> <script language = "javascript" type = "text / javascript" src = "controlemo.js"> </ script></p> <p><script language = "javaScript" type = "text / javascript"></p> <p>Var demo = new controlemo ();</p> <p></ script></p> <p>Is it very simple in the code on the feel?</p> <p>Almost just place objects and construct classes, don't do anything else.</p> <p>Do you have a friend who uses ASP.NET with a friend who has written a background code?</p> <p>Although I don't dare to say it, this is the alternative power of JavaScript. ^^</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-47869.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="47869" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.035</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'oG0meS0Q4Vgm7ZTk2AuPUGkqHc4159yxsvHgN0WOHZR9ar_2B68_2FZwqANgCEezgjc7SwXZWrKEy4nrCUr4K5GgFg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>