JavaScript Function.prototype.toSource() 在 Chrome 不工作

JavaScript Function.prototype.toSource() in Chrome not working

我对下面的脚本有疑问。我在 Chrome 中收到 toSource is not defined 错误,并发现 toSource() 是 Firefox 特定的。我尝试了 JSON.stringify(),但出现了未定义的错误。

var main_tbls = d.querySelectorAll(".bz_buglist");
var first_tbl = main_tbls[0];
first_tbl.id = "bugz_table";
var ext_script = d.createElement("script");
//ext_script.setAttribute("src", "http://www.javascriptkit.com/script/script2/tablefilter.js");
ext_script.setAttribute("type", "text/javascript");
ext_script.setAttribute("language", "javascript");
ext_script.appendChild(d.createTextNode("var TblId, SearchFlt, SlcArgs;TblId = new Array(), SlcArgs = new Array();var colValues = new Array();" +
    setFilterGrid.toSource() +
    AddGrid.toSource() ));
d.body.appendChild(ext_script);


function setFilterGrid(id)
{
    var tbl = grabEBI(id);
    var ref_row, fObj;
    if (tbl != null && tbl.nodeName.toLowerCase() == "table") {
        if (arguments.length > 1) {
            for (var i = 0; i < arguments.length; i++) {
                var argtype = typeof arguments[i];

                switch (argtype.toLowerCase()) {
                    case "number":
                        ref_row = arguments[i];
                        break;
                    case "object":
                        fObj = arguments[i];
                        break;
                } //switch

            } //for
        } //if

        ref_row == undefined ? ref_row = 2 : ref_row = (ref_row + 2);
        var ncells = getCellsNb(id, ref_row);
        tbl.tf_ncells = ncells;
        if (tbl.tf_ref_row == undefined) tbl.tf_ref_row = ref_row;
        tbl.tf_Obj = fObj;
        if (!hasGrid(id)) AddGrid(id);
    } //if tbl!=null
}

虽然您的问题不清楚,但您似乎正在寻找 Function.prototype.toString() method. This will work in basically all browsers

document.body.innerHTML =  '<pre>' + setFilterGrid.toString() +'</pre>';


function setFilterGrid(id)
{
    var tbl = grabEBI(id);
    var ref_row, fObj;
    if (tbl != null && tbl.nodeName.toLowerCase() == "table") {
        if (arguments.length > 1) {
            for (var i = 0; i < arguments.length; i++) {
                var argtype = typeof arguments[i];

                switch (argtype.toLowerCase()) {
                    case "number":
                        ref_row = arguments[i];
                        break;
                    case "object":
                        fObj = arguments[i];
                        break;
                } //switch

            } //for
        } //if

        ref_row == undefined ? ref_row = 2 : ref_row = (ref_row + 2);
        var ncells = getCellsNb(id, ref_row);
        tbl.tf_ncells = ncells;
        if (tbl.tf_ref_row == undefined) tbl.tf_ref_row = ref_row;
        tbl.tf_Obj = fObj;
        if (!hasGrid(id)) AddGrid(id);
    } //if tbl!=null
}

Function.prototype.toSource(), on the other hand, is only available in Firefox。 Mozilla的文档写的很清楚是:

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.