posts - 28,  comments - 15,  trackbacks - 0
          今天在看完xtree.js代碼后,發現一個問題。在執行javascript的document.write(obj)方法時,如果obj實現了prototype的toString()方法,document.wirte方法將能自動調用obj的toString()方法。代碼如下:
          ???/*----------?? 樹構造代碼 ---------*/
          ??????? if (document.getElementById) {
          ?????????? var tree = new WebFXTree('Root');
          ?????????? tree.setBehavior('classic');
          ?????????? var a = new WebFXTreeItem('1');
          ?????????? tree.add(a);
          ?????????? var b = new WebFXTreeItem('1.1');
          ?????????? a.add(b);
          ????????? b.add(new WebFXTreeItem('1.1.1'));
          ????????? b.add(new WebFXTreeItem('1.1.2'));
          ????????? b.add(new WebFXTreeItem('1.1.3'));
          ????????? var f = new WebFXTreeItem('1.1.4');
          ????????? b.add(f);

          ???????? document.write(tree);

          /*------------------ xtree.js代碼片段 ----------*/

          WebFXTree.prototype.toString = function() {
          alert("it's here!");
          ?var str = "<div id=\"" + this.id + "\" ondblclick=\"webFXTreeHandler.toggle(this);\" class=\"webfx-tree-item\" onkeydown=\"return webFXTreeHandler.keydown(this, event)\">" +
          ??"<img id=\"" + this.id + "-icon\" class=\"webfx-tree-icon\" src=\"" + ((webFXTreeHandler.behavior == 'classic' && this.open)?this.openIcon:this.icon) + "\" onclick=\"webFXTreeHandler.select(this);\">" +
          ??"<a href=\"" + this.action + "\" id=\"" + this.id + "-anchor\" onfocus=\"webFXTreeHandler.focus(this);\" onblur=\"webFXTreeHandler.blur(this);\"" +
          ??(this.target ? " target=\"" + this.target + "\"" : "") +
          ??">" + this.text + "</a></div>" +
          ??"<div id=\"" + this.id + "-cont\" class=\"webfx-tree-container\" style=\"display: " + ((this.open)?'block':'none') + ";\">";
          ?var sb = [];
          ?for (var i = 0; i < this.childNodes.length; i++) {
          ??sb[i] = this.childNodes[i].toString(i, this.childNodes.length);
          ?}
          ?this.rendered = true;
          ?return str + sb.join("") + "</div>";
          };

          經過跟蹤,證實在執行document.write(tree)時,調用了WebFXTree.prototype.toString 方法。
          posted on 2009-08-13 16:20 zhangxl 閱讀(1020) 評論(1)  編輯  收藏


          FeedBack:
          # re: JavaScript的toString()方法自動調用
          2011-03-14 09:10 |
          更呼庚呼癸  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2009年8月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(1)

          隨筆分類(17)

          隨筆檔案(28)

          文章分類(30)

          文章檔案(30)

          相冊

          收藏夾(2)

          hibernate

          java基礎

          mysql

          xml

          關注

          壓力測試

          算法

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 96772
          • 排名 - 600

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 聂拉木县| 邹平县| 石河子市| 汨罗市| 尚志市| 延吉市| 仁怀市| 大同市| 余江县| 龙游县| 庆城县| 西贡区| 麟游县| 萨迦县| 洪雅县| 双峰县| 綦江县| 博兴县| 建宁县| 凤山市| 察雅县| 诏安县| 皮山县| 灌阳县| 金山区| 兴义市| 望奎县| 凯里市| 宣武区| 金川县| 南和县| 德化县| 乐至县| 恭城| 藁城市| 石楼县| 漠河县| 盘锦市| 遂昌县| 逊克县| 无极县|