Encountered the problem of a JS prototype, has not been resolved

Source: Internet
Author: User

There are some problems with writing code today. First of all, my HTML file is like this

<= "Tag_add"  onclick= "AddUser ()"></Div  >

NN, it is clear that the AddUser () method was called

varAddUser =function () {    varI=0;Document.onkeydown=function(e) {varEV = document.all?window.event:e; if(ev.keycode==13) {            Submit_dialog (); }            if(ev.keycode==27) {Quit_dialog (); }        }    functionQuit_dialog () {
/* Hide Dialog div*/} $ ("#head_close, #bottom_canser"). Click (function() {quit_dialog (); }); functionSubmit_dialog () {Alert (Name+phone+Plantno); } $("#bottom_submit"). Click (function() {submit_dialog (); });}

First call, with the mouse point the submit, no problem; second call, eh? How did alert pop up two times?? Mother of the Egg! Third call, OK, alert pops up three times!

I immediately thought of the onclick call function is not every time you want to create a new function instance? Java EE has a single prototype concept, put in this case, it should be JS interpreter every time a new function object of a single case? This problem occurs when the previous singleton is not closed and then a singleton is called?? (here to solve AH!!) )

So I added a code to prove

varAddUser =function () {    varI=0; Document.onkeydown=function(e) {varEV = document.all?window.event:e; if(ev.keycode==13) {submit_dialog (); }            if(ev.keycode==27) {quit_dialog (); }        }    functionQuit_dialog () {/*hiding the dialog div .*/    }    $("#head_close, #bottom_canser"). Click (function() {quit_dialog ();    }); functionSubmit_dialog () {Alert (name+phone+Plantno); }    $("#bottom_submit"). Click (function() {submit_dialog (); I++;    Console.log (i); });}

And look at my control panel.

Yes, just like I thought! It is true that a new instance has been created. Just as I was sure this was the case, I found out that these problems won't happen with a button trigger! Is it because var addUser = function () {} is a prototype for every visit? Do not understand the solution Ah!

It is easy to think of the search for JS prototype How to use, prototype. I've heard a bit about it before and I forgot it for a long time ...

The problem has not been solved, if there is a master answer greatly appreciated Ah!

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.