官术网_书友最值得收藏!

Solution #6 - redefine the handler

Another idea: instead of changing anything in the button, let's have the event handler change itself. The trick is in the second line; by assigning a new value to the billTheUser variable, we are actually dynamically changing what the function does! The first time you call the function, it will do its thing... but it will also change itself out of existence, by giving its name to a new function:

function billTheUser(some, sales, data) {
billTheUser = function() {};
window.alert("Billing the user...");
// actually bill the user
}

There's a special trick in the solution. Functions are global, so the line billTheUser=... actually changes the function's inner workings; from that point on, billTheUser will be the new (null) function. This solution is still hard to test. Even worse, how would you restore the functionality of billTheUser, setting it back to its original objective?

主站蜘蛛池模板: 庄河市| 长乐市| 读书| 茶陵县| 会昌县| 襄城县| 咸丰县| 霞浦县| 沙雅县| 凯里市| 宁南县| 当阳市| 佳木斯市| 来凤县| 临西县| 凭祥市| 桐乡市| 华蓥市| 左云县| 潮安县| 吴忠市| 海安县| 韶山市| 十堰市| 佛坪县| 乌拉特中旗| 商都县| 梁山县| 赤峰市| 南投县| 黑山县| 葫芦岛市| 罗田县| 娄底市| 大渡口区| 明水县| 大庆市| 沅江市| 武隆县| 长泰县| 漠河县|