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

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?

主站蜘蛛池模板: 巴林左旗| 清原| 集贤县| 莲花县| 夏邑县| 舟山市| 丰都县| 屏东县| 平乐县| 陇南市| 芷江| 南漳县| 综艺| 阳高县| 仪陇县| 黔南| 海门市| 永定县| 田阳县| 双柏县| 洛川县| 含山县| 东台市| 芒康县| 甘德县| 姚安县| 武义县| 巴马| 小金县| 呼玛县| 搜索| 仁化县| 昆山市| 德钦县| 陵川县| 临澧县| 金阳县| 中阳县| 濮阳县| 耿马| 武平县|