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 閱讀(1015) 評論(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

          關注

          壓力測試

          算法

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 96364
          • 排名 - 601

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 永州市| 手游| 长丰县| 贺州市| 全南县| 马边| 新晃| 江永县| 恩平市| 扶风县| 江孜县| 宁津县| 郓城县| 农安县| 巴林左旗| 漯河市| 久治县| 新源县| 连平县| 台前县| 天祝| 三台县| 克什克腾旗| 新营市| 梧州市| 东乌珠穆沁旗| 新和县| 平顺县| 安庆市| 榆林市| 固安县| 呼和浩特市| 普安县| 宣威市| 历史| 吉木乃县| 荃湾区| 文山县| 会泽县| 平原县| 阿尔山市|