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

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?

主站蜘蛛池模板: 绿春县| 吉木乃县| 昂仁县| 宝鸡市| 容城县| 临沂市| 瓮安县| 太仆寺旗| 察哈| 潼南县| 武邑县| 桃园市| 当雄县| 登封市| 绵阳市| 白银市| 广丰县| 乌拉特中旗| 伊春市| 寻甸| 玛沁县| 沅江市| 奉贤区| 岳阳市| 清苑县| 罗甸县| 大厂| 邹城市| 澎湖县| 赫章县| 营口市| 永川市| 皋兰县| 二手房| 泸溪县| 剑阁县| 大关县| 邢台市| 禄丰县| 镇康县| 德昌县|