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

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?

主站蜘蛛池模板: 青川县| 常德市| 那坡县| 海盐县| 乾安县| 昌都县| 揭东县| 建湖县| 本溪市| 台前县| 公安县| 呼图壁县| 抚松县| 都江堰市| 玉林市| 哈密市| 石棉县| 福泉市| 太谷县| 延川县| 麻江县| 平和县| 柳江县| 绥德县| 图木舒克市| 集贤县| 屯留县| 田林县| 齐河县| 东平县| 长寿区| 仁怀市| 巴楚县| 南木林县| 石泉县| 宜宾县| 安宁市| 抚州市| 湖州市| 兴安县| 方山县|