posts - 28,  comments - 15,  trackbacks - 0
          今天在看完xtree.js代碼后,發(fā)現(xiàn)一個(gè)問題。在執(zhí)行javascript的document.write(obj)方法時(shí),如果obj實(shí)現(xiàn)了prototype的toString()方法,document.wirte方法將能自動(dòng)調(diào)用obj的toString()方法。代碼如下:
          ???/*----------?? 樹構(gòu)造代碼 ---------*/
          ??????? 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>";
          };

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


          FeedBack:
          # re: JavaScript的toString()方法自動(dòng)調(diào)用
          2011-03-14 09:10 |
          更呼庚呼癸  回復(fù)  更多評論
            

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


          網(wǎng)站導(dǎo)航:
           
          <2011年3月>
          272812345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類(17)

          隨筆檔案(28)

          文章分類(30)

          文章檔案(30)

          相冊

          收藏夾(2)

          hibernate

          java基礎(chǔ)

          mysql

          xml

          關(guān)注

          壓力測試

          算法

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 96750
          • 排名 - 600

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 延吉市| 光山县| 额尔古纳市| 三门县| 公安县| 兴和县| 青神县| 房山区| 洛宁县| 乐亭县| 沁源县| 亳州市| 宁陵县| 商河县| 南京市| 曲沃县| 原阳县| 明光市| 洮南市| 大同市| 伽师县| 贞丰县| 定远县| 新闻| 山丹县| 二手房| 郧西县| 望江县| 桑日县| 建瓯市| 三亚市| 津南区| 临沂市| 门头沟区| 团风县| 洱源县| 南平市| 沂水县| 京山县| 凤山县| 延庆县|