代码之家  ›  专栏  ›  技术社区  ›  Adriana

PHP中的var_dump或print_r的JavaScript等价物是什么?[副本]

  •  267
  • Adriana  · 技术社区  · 16 年前

    我希望看到JavaScript中对象的结构(用于调试)。PHP中是否有类似于var_dump的内容?

    10 回复  |  直到 10 年前
        1
  •  318
  •   Jonah    9 年前

    console.log(myvar);
    

    然后,您将在控制台中获得对象/任何对象的一个映射良好的接口。

    查看 console 有关更多详细信息,请参阅文档。

        2
  •  169
  •   Francesco Casula    10 年前

    最常见的方式:

    console.log(object);
    

    但是我必须提到 JSON.stringify 这对于在非浏览器脚本中转储变量很有用:

    console.log( JSON.stringify(object) );
    

    这个 JSON.stringify 函数还支持内置的修饰,正如

    例子:

    var obj = {x: 1, y: 2, z: 3};
    
    console.log( JSON.stringify(obj, null, 2) ); // spacing level = 2
    

    上面的代码段将打印:

    {
      "x": 1,
      "y": 2,
      "z": 3
    }
    

    在…上 caniuse.com 您可以查看本机支持的浏览器 JSON.stringify 功能: http://caniuse.com/json

    您还可以使用Douglas Crockford库添加 JSON.stringify 旧浏览器上的支持: https://github.com/douglascrockford/JSON-js

    文件 JSON.stringify : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

    我希望这有帮助:-)

        3
  •  96
  •   nurdyguy    5 年前

    我写了这个JS函数 dump() 像PHP一样工作 var_dump() . 要在警报窗口中显示变量的内容,请执行以下操作: dump(variable) dump(variable, 'body') 要仅获取变量的字符串,请执行以下操作: dump(variable, 'none')

    /* repeatString() returns a string which has been repeated a set number of times */
    function repeatString(str, num) {
        out = '';
        for (var i = 0; i < num; i++) {
            out += str;
        }
        return out;
    }
    
    /*
    dump() displays the contents of a variable like var_dump() does in PHP. dump() is
    better than typeof, because it can distinguish between array, null and object.
    Parameters:
        v:              The variable
        howDisplay:     "none", "body", "alert" (default)
        recursionLevel: Number of times the function has recursed when entering nested
                        objects or arrays. Each level of recursion adds extra space to the
                        output to indicate level. Set to 0 by default.
    Return Value:
        A string of the variable's contents
    Limitations:
        Can't pass an undefined variable to dump(). 
        dump() can't distinguish between int and float.
        dump() can't tell the original variable type of a member variable of an object.
        These limitations can't be fixed because these are *features* of JS. However, dump()
    */
    function dump(v, howDisplay, recursionLevel) {
        howDisplay = (typeof howDisplay === 'undefined') ? "alert" : howDisplay;
        recursionLevel = (typeof recursionLevel !== 'number') ? 0 : recursionLevel;
    
        var vType = typeof v;
        var out = vType;
    
        switch (vType) {
            case "number":
            /* there is absolutely no way in JS to distinguish 2 from 2.0
               so 'number' is the best that you can do. The following doesn't work:
               var er = /^[0-9]+$/;
               if (!isNaN(v) && v % 1 === 0 && er.test(3.0)) {
                   out = 'int';
               }
            */
            break;
        case "boolean":
            out += ": " + v;
            break;
        case "string":
            out += "(" + v.length + '): "' + v + '"';
            break;
        case "object":
            //check if null
            if (v === null) {
                out = "null";
            }
            //If using jQuery: if ($.isArray(v))
            //If using IE: if (isArray(v))
            //this should work for all browsers according to the ECMAScript standard:
            else if (Object.prototype.toString.call(v) === '[object Array]') {
                out = 'array(' + v.length + '): {\n';
                for (var i = 0; i < v.length; i++) {
                    out += repeatString('   ', recursionLevel) + "   [" + i + "]:  " +
                        dump(v[i], "none", recursionLevel + 1) + "\n";
                }
                out += repeatString('   ', recursionLevel) + "}";
            }
            else {
                //if object
                let sContents = "{\n";
                let cnt = 0;
                for (var member in v) {
                    //No way to know the original data type of member, since JS
                    //always converts it to a string and no other way to parse objects.
                    sContents += repeatString('   ', recursionLevel) + "   " + member +
                        ":  " + dump(v[member], "none", recursionLevel + 1) + "\n";
                    cnt++;
                }
                sContents += repeatString('   ', recursionLevel) + "}";
                out += "(" + cnt + "): " + sContents;
            }
            break;
        default:
            out = v;
            break;
        }
    
        if (howDisplay == 'body') {
            var pre = document.createElement('pre');
            pre.innerHTML = out;
            document.body.appendChild(pre);
        }
        else if (howDisplay == 'alert') {
            alert(out);
        }
    
        return out;
    }
    
        4
  •  39
  •   Azder    13 年前

    JavaScript中的var_dump等价物?简单地说,没有一个。

    但是,这并不意味着你会束手无策。就像一些人建议的那样,使用 Firebug (或其他浏览器中的等效工具),但与其他人建议的不同,当您有(稍微)更好的工具时,不要使用console.log console.dir :

    console.dir(object)
    

    打印对象所有属性的交互式列表。这 看起来与在DOM选项卡中看到的视图相同。

        5
  •  29
  •   Sk8erPeter miku    9 年前

    正如其他人已经提到的,调试变量的最佳方法是使用现代浏览器的开发人员控制台(例如。 Chrome Developer Tools ,Firefox+ Firebug Opera Dragonfly say , “蜻蜓还没死,虽然我们还不能给你更多的信息” .

    但如果你需要另一种方法,有一个非常有用的网站叫做 :

    http://phpjs.org/

    提供 -因此,您可以像在PHP中一样使用它们。我将在这里复制粘贴适当的函数给您,但是 请注意,如果检测到一些bug,这些代码可以在原始站点上更新,因此我建议您访问 phpjs.org 地点

    var_dump() 在JavaScript中

    以下是JS替代方案的代码 var_dump() :
    http://phpjs.org/functions/var_dump/
    echo() 功能: http://phpjs.org/functions/echo/

    function var_dump() {
      //  discuss at: http://phpjs.org/functions/var_dump/
      // original by: Brett Zamir (http://brett-zamir.me)
      // improved by: Zahlii
      // improved by: Brett Zamir (http://brett-zamir.me)
      //  depends on: echo
      //        note: For returning a string, use var_export() with the second argument set to true
      //        test: skip
      //   example 1: var_dump(1);
      //   returns 1: 'int(1)'
    
      var output = '',
        pad_char = ' ',
        pad_val = 4,
        lgth = 0,
        i = 0;
    
      var _getFuncName = function(fn) {
        var name = (/\W*function\s+([\w\$]+)\s*\(/)
          .exec(fn);
        if (!name) {
          return '(Anonymous)';
        }
        return name[1];
      };
    
      var _repeat_char = function(len, pad_char) {
        var str = '';
        for (var i = 0; i < len; i++) {
          str += pad_char;
        }
        return str;
      };
      var _getInnerVal = function(val, thick_pad) {
        var ret = '';
        if (val === null) {
          ret = 'NULL';
        } else if (typeof val === 'boolean') {
          ret = 'bool(' + val + ')';
        } else if (typeof val === 'string') {
          ret = 'string(' + val.length + ') "' + val + '"';
        } else if (typeof val === 'number') {
          if (parseFloat(val) == parseInt(val, 10)) {
            ret = 'int(' + val + ')';
          } else {
            ret = 'float(' + val + ')';
          }
        }
        // The remaining are not PHP behavior because these values only exist in this exact form in JavaScript
        else if (typeof val === 'undefined') {
          ret = 'undefined';
        } else if (typeof val === 'function') {
          var funcLines = val.toString()
            .split('\n');
          ret = '';
          for (var i = 0, fll = funcLines.length; i < fll; i++) {
            ret += (i !== 0 ? '\n' + thick_pad : '') + funcLines[i];
          }
        } else if (val instanceof Date) {
          ret = 'Date(' + val + ')';
        } else if (val instanceof RegExp) {
          ret = 'RegExp(' + val + ')';
        } else if (val.nodeName) {
          // Different than PHP's DOMElement
          switch (val.nodeType) {
          case 1:
            if (typeof val.namespaceURI === 'undefined' || val.namespaceURI === 'http://www.w3.org/1999/xhtml') {
              // Undefined namespace could be plain XML, but namespaceURI not widely supported
              ret = 'HTMLElement("' + val.nodeName + '")';
            } else {
              ret = 'XML Element("' + val.nodeName + '")';
            }
            break;
          case 2:
            ret = 'ATTRIBUTE_NODE(' + val.nodeName + ')';
            break;
          case 3:
            ret = 'TEXT_NODE(' + val.nodeValue + ')';
            break;
          case 4:
            ret = 'CDATA_SECTION_NODE(' + val.nodeValue + ')';
            break;
          case 5:
            ret = 'ENTITY_REFERENCE_NODE';
            break;
          case 6:
            ret = 'ENTITY_NODE';
            break;
          case 7:
            ret = 'PROCESSING_INSTRUCTION_NODE(' + val.nodeName + ':' + val.nodeValue + ')';
            break;
          case 8:
            ret = 'COMMENT_NODE(' + val.nodeValue + ')';
            break;
          case 9:
            ret = 'DOCUMENT_NODE';
            break;
          case 10:
            ret = 'DOCUMENT_TYPE_NODE';
            break;
          case 11:
            ret = 'DOCUMENT_FRAGMENT_NODE';
            break;
          case 12:
            ret = 'NOTATION_NODE';
            break;
          }
        }
        return ret;
      };
    
      var _formatArray = function(obj, cur_depth, pad_val, pad_char) {
        var someProp = '';
        if (cur_depth > 0) {
          cur_depth++;
        }
    
        var base_pad = _repeat_char(pad_val * (cur_depth - 1), pad_char);
        var thick_pad = _repeat_char(pad_val * (cur_depth + 1), pad_char);
        var str = '';
        var val = '';
    
        if (typeof obj === 'object' && obj !== null) {
          if (obj.constructor && _getFuncName(obj.constructor) === 'PHPJS_Resource') {
            return obj.var_dump();
          }
          lgth = 0;
          for (someProp in obj) {
            lgth++;
          }
          str += 'array(' + lgth + ') {\n';
          for (var key in obj) {
            var objVal = obj[key];
            if (typeof objVal === 'object' && objVal !== null && !(objVal instanceof Date) && !(objVal instanceof RegExp) &&
              !
              objVal.nodeName) {
              str += thick_pad + '[' + key + '] =>\n' + thick_pad + _formatArray(objVal, cur_depth + 1, pad_val,
                pad_char);
            } else {
              val = _getInnerVal(objVal, thick_pad);
              str += thick_pad + '[' + key + '] =>\n' + thick_pad + val + '\n';
            }
          }
          str += base_pad + '}\n';
        } else {
          str = _getInnerVal(obj, thick_pad);
        }
        return str;
      };
    
      output = _formatArray(arguments[0], 0, pad_val, pad_char);
      for (i = 1; i < arguments.length; i++) {
        output += '\n' + _formatArray(arguments[i], 0, pad_val, pad_char);
      }
    
      this.echo(output);
    }
    

    print_r() 在JavaScript中

    这是你的电话号码 列印 功能:
    http://phpjs.org/functions/print_r/

    function print_r(array, return_val) {
      //  discuss at: http://phpjs.org/functions/print_r/
      // original by: Michael White (http://getsprink.com)
      // improved by: Ben Bryan
      // improved by: Brett Zamir (http://brett-zamir.me)
      // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
      //    input by: Brett Zamir (http://brett-zamir.me)
      //  depends on: echo
      //   example 1: print_r(1, true);
      //   returns 1: 1
    
      var output = '',
        pad_char = ' ',
        pad_val = 4,
        d = this.window.document,
        getFuncName = function(fn) {
          var name = (/\W*function\s+([\w\$]+)\s*\(/)
            .exec(fn);
          if (!name) {
            return '(Anonymous)';
          }
          return name[1];
        };
      repeat_char = function(len, pad_char) {
        var str = '';
        for (var i = 0; i < len; i++) {
          str += pad_char;
        }
        return str;
      };
      formatArray = function(obj, cur_depth, pad_val, pad_char) {
        if (cur_depth > 0) {
          cur_depth++;
        }
    
        var base_pad = repeat_char(pad_val * cur_depth, pad_char);
        var thick_pad = repeat_char(pad_val * (cur_depth + 1), pad_char);
        var str = '';
    
        if (typeof obj === 'object' && obj !== null && obj.constructor && getFuncName(obj.constructor) !==
          'PHPJS_Resource') {
          str += 'Array\n' + base_pad + '(\n';
          for (var key in obj) {
            if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
              str += thick_pad + '[' + key + '] => ' + formatArray(obj[key], cur_depth + 1, pad_val, pad_char);
            } else {
              str += thick_pad + '[' + key + '] => ' + obj[key] + '\n';
            }
          }
          str += base_pad + ')\n';
        } else if (obj === null || obj === undefined) {
          str = '';
        } else {
          // for our "resource" class
          str = obj.toString();
        }
    
        return str;
      };
    
      output = formatArray(array, 0, pad_val, pad_char);
    
      if (return_val !== true) {
        if (d.body) {
          this.echo(output);
        } else {
          try {
            // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
            d = XULDocument;
            this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">' + output + '</pre>');
          } catch (e) {
            // Outputting as plain text may work in some plain XML
            this.echo(output);
          }
        }
        return true;
      }
      return output;
    }
    

    var_export() 在JavaScript中

    您还可以找到 备选方案是否有用,这也取决于 回声()
    http://phpjs.org/functions/var_export/

    function var_export(mixed_expression, bool_return) {
      //  discuss at: http://phpjs.org/functions/var_export/
      // original by: Philip Peterson
      // improved by: johnrembo
      // improved by: Brett Zamir (http://brett-zamir.me)
      //    input by: Brian Tafoya (http://www.premasolutions.com/)
      //    input by: Hans Henrik (http://hanshenrik.tk/)
      // bugfixed by: Brett Zamir (http://brett-zamir.me)
      // bugfixed by: Brett Zamir (http://brett-zamir.me)
      //  depends on: echo
      //   example 1: var_export(null);
      //   returns 1: null
      //   example 2: var_export({0: 'Kevin', 1: 'van', 2: 'Zonneveld'}, true);
      //   returns 2: "array (\n  0 => 'Kevin',\n  1 => 'van',\n  2 => 'Zonneveld'\n)"
      //   example 3: data = 'Kevin';
      //   example 3: var_export(data, true);
      //   returns 3: "'Kevin'"
    
      var retstr = '',
        iret = '',
        value,
        cnt = 0,
        x = [],
        i = 0,
        funcParts = [],
        // We use the last argument (not part of PHP) to pass in
        // our indentation level
        idtLevel = arguments[2] || 2,
        innerIndent = '',
        outerIndent = '',
        getFuncName = function(fn) {
          var name = (/\W*function\s+([\w\$]+)\s*\(/)
            .exec(fn);
          if (!name) {
            return '(Anonymous)';
          }
          return name[1];
        };
      _makeIndent = function(idtLevel) {
        return (new Array(idtLevel + 1))
          .join(' ');
      };
      __getType = function(inp) {
        var i = 0,
          match, types, cons, type = typeof inp;
        if (type === 'object' && (inp && inp.constructor) &&
          getFuncName(inp.constructor) === 'PHPJS_Resource') {
          return 'resource';
        }
        if (type === 'function') {
          return 'function';
        }
        if (type === 'object' && !inp) {
          // Should this be just null?
          return 'null';
        }
        if (type === 'object') {
          if (!inp.constructor) {
            return 'object';
          }
          cons = inp.constructor.toString();
          match = cons.match(/(\w+)\(/);
          if (match) {
            cons = match[1].toLowerCase();
          }
          types = ['boolean', 'number', 'string', 'array'];
          for (i = 0; i < types.length; i++) {
            if (cons === types[i]) {
              type = types[i];
              break;
            }
          }
        }
        return type;
      };
      type = __getType(mixed_expression);
    
      if (type === null) {
        retstr = 'NULL';
      } else if (type === 'array' || type === 'object') {
        outerIndent = _makeIndent(idtLevel - 2);
        innerIndent = _makeIndent(idtLevel);
        for (i in mixed_expression) {
          value = this.var_export(mixed_expression[i], 1, idtLevel + 2);
          value = typeof value === 'string' ? value.replace(/</g, '&lt;')
            .
          replace(/>/g, '&gt;'): value;
          x[cnt++] = innerIndent + i + ' => ' +
            (__getType(mixed_expression[i]) === 'array' ?
              '\n' : '') + value;
        }
        iret = x.join(',\n');
        retstr = outerIndent + 'array (\n' + iret + '\n' + outerIndent + ')';
      } else if (type === 'function') {
        funcParts = mixed_expression.toString()
          .
        match(/function .*?\((.*?)\) \{([\s\S]*)\}/);
    
        // For lambda functions, var_export() outputs such as the following:
        // '\000lambda_1'. Since it will probably not be a common use to
        // expect this (unhelpful) form, we'll use another PHP-exportable
        // construct, create_function() (though dollar signs must be on the
        // variables in JavaScript); if using instead in JavaScript and you
        // are using the namespaced version, note that create_function() will
        // not be available as a global
        retstr = "create_function ('" + funcParts[1] + "', '" +
          funcParts[2].replace(new RegExp("'", 'g'), "\\'") + "')";
      } else if (type === 'resource') {
        // Resources treated as null for var_export
        retstr = 'NULL';
      } else {
        retstr = typeof mixed_expression !== 'string' ? mixed_expression :
          "'" + mixed_expression.replace(/(["'])/g, '\\$1')
          .
        replace(/\0/g, '\\0') + "'";
      }
    
      if (!bool_return) {
        this.echo(retstr);
        return null;
      }
    
      return retstr;
    }
    

    回声()

    http://phpjs.org/functions/echo/

    function echo() {
      //  discuss at: http://phpjs.org/functions/echo/
      // original by: Philip Peterson
      // improved by: echo is bad
      // improved by: Nate
      // improved by: Brett Zamir (http://brett-zamir.me)
      // improved by: Brett Zamir (http://brett-zamir.me)
      // improved by: Brett Zamir (http://brett-zamir.me)
      //  revised by: Der Simon (http://innerdom.sourceforge.net/)
      // bugfixed by: Eugene Bulkin (http://doubleaw.com/)
      // bugfixed by: Brett Zamir (http://brett-zamir.me)
      // bugfixed by: Brett Zamir (http://brett-zamir.me)
      // bugfixed by: EdorFaus
      //    input by: JB
      //        note: If browsers start to support DOM Level 3 Load and Save (parsing/serializing),
      //        note: we wouldn't need any such long code (even most of the code below). See
      //        note: link below for a cross-browser implementation in JavaScript. HTML5 might
      //        note: possibly support DOMParser, but that is not presently a standard.
      //        note: Although innerHTML is widely used and may become standard as of HTML5, it is also not ideal for
      //        note: use with a temporary holder before appending to the DOM (as is our last resort below),
      //        note: since it may not work in an XML context
      //        note: Using innerHTML to directly add to the BODY is very dangerous because it will
      //        note: break all pre-existing references to HTMLElements.
      //   example 1: echo('<div><p>abc</p><p>abc</p></div>');
      //   returns 1: undefined
    
      var isNode = typeof module !== 'undefined' && module.exports && typeof global !== "undefined" && {}.toString.call(
        global) == '[object global]';
      if (isNode) {
        var args = Array.prototype.slice.call(arguments);
        return console.log(args.join(' '));
      }
    
      var arg = '';
      var argc = arguments.length;
      var argv = arguments;
      var i = 0;
      var holder, win = this.window;
      var d = win.document;
      var ns_xhtml = 'http://www.w3.org/1999/xhtml';
      // If we're in a XUL context
      var ns_xul = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
    
      var stringToDOM = function(str, parent, ns, container) {
        var extraNSs = '';
        if (ns === ns_xul) {
          extraNSs = ' xmlns:html="' + ns_xhtml + '"';
        }
        var stringContainer = '<' + container + ' xmlns="' + ns + '"' + extraNSs + '>' + str + '</' + container + '>';
        var dils = win.DOMImplementationLS;
        var dp = win.DOMParser;
        var ax = win.ActiveXObject;
        if (dils && dils.createLSInput && dils.createLSParser) {
          // Follows the DOM 3 Load and Save standard, but not
          // implemented in browsers at present; HTML5 is to standardize on innerHTML, but not for XML (though
          // possibly will also standardize with DOMParser); in the meantime, to ensure fullest browser support, could
          // attach http://svn2.assembla.com/svn/brettz9/DOMToString/DOM3.js (see http://svn2.assembla.com/svn/brettz9/DOMToString/DOM3.xhtml for a simple test file)
          var lsInput = dils.createLSInput();
          // If we're in XHTML, we'll try to allow the XHTML namespace to be available by default
          lsInput.stringData = stringContainer;
          // synchronous, no schema type
          var lsParser = dils.createLSParser(1, null);
          return lsParser.parse(lsInput)
            .firstChild;
        } else if (dp) {
          // If we're in XHTML, we'll try to allow the XHTML namespace to be available by default
          try {
            var fc = new dp()
              .parseFromString(stringContainer, 'text/xml');
            if (fc && fc.documentElement && fc.documentElement.localName !== 'parsererror' && fc.documentElement.namespaceURI !==
              'http://www.mozilla.org/newlayout/xml/parsererror.xml') {
              return fc.documentElement.firstChild;
            }
            // If there's a parsing error, we just continue on
          } catch (e) {
            // If there's a parsing error, we just continue on
          }
        } else if (ax) {
          // We don't bother with a holder in Explorer as it doesn't support namespaces
          var axo = new ax('MSXML2.DOMDocument');
          axo.loadXML(str);
          return axo.documentElement;
        }
        /*else if (win.XMLHttpRequest) {
         // Supposed to work in older Safari
          var req = new win.XMLHttpRequest;
          req.open('GET', 'data:application/xml;charset=utf-8,'+encodeURIComponent(str), false);
          if (req.overrideMimeType) {
            req.overrideMimeType('application/xml');
          }
          req.send(null);
          return req.responseXML;
        }*/
        // Document fragment did not work with innerHTML, so we create a temporary element holder
        // If we're in XHTML, we'll try to allow the XHTML namespace to be available by default
        //if (d.createElementNS && (d.contentType && d.contentType !== 'text/html')) {
        // Don't create namespaced elements if we're being served as HTML (currently only Mozilla supports this detection in true XHTML-supporting browsers, but Safari and Opera should work with the above DOMParser anyways, and IE doesn't support createElementNS anyways)
        if (d.createElementNS && // Browser supports the method
          (d.documentElement.namespaceURI || // We can use if the document is using a namespace
            d.documentElement.nodeName.toLowerCase() !== 'html' || // We know it's not HTML4 or less, if the tag is not HTML (even if the root namespace is null)
            (d.contentType && d.contentType !== 'text/html') // We know it's not regular HTML4 or less if this is Mozilla (only browser supporting the attribute) and the content type is something other than text/html; other HTML5 roots (like svg) still have a namespace
          )) {
          // Don't create namespaced elements if we're being served as HTML (currently only Mozilla supports this detection in true XHTML-supporting browsers, but Safari and Opera should work with the above DOMParser anyways, and IE doesn't support createElementNS anyways); last test is for the sake of being in a pure XML document
          holder = d.createElementNS(ns, container);
        } else {
          // Document fragment did not work with innerHTML
          holder = d.createElement(container);
        }
        holder.innerHTML = str;
        while (holder.firstChild) {
          parent.appendChild(holder.firstChild);
        }
        return false;
        // throw 'Your browser does not support DOM parsing as required by echo()';
      };
    
      var ieFix = function(node) {
        if (node.nodeType === 1) {
          var newNode = d.createElement(node.nodeName);
          var i, len;
          if (node.attributes && node.attributes.length > 0) {
            for (i = 0, len = node.attributes.length; i < len; i++) {
              newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i].nodeName));
            }
          }
          if (node.childNodes && node.childNodes.length > 0) {
            for (i = 0, len = node.childNodes.length; i < len; i++) {
              newNode.appendChild(ieFix(node.childNodes[i]));
            }
          }
          return newNode;
        } else {
          return d.createTextNode(node.nodeValue);
        }
      };
    
      var replacer = function(s, m1, m2) {
        // We assume for now that embedded variables do not have dollar sign; to add a dollar sign, you currently must use {$$var} (We might change this, however.)
        // Doesn't cover all cases yet: see http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
        if (m1 !== '\\') {
          return m1 + eval(m2);
        } else {
          return s;
        }
      };
    
      this.php_js = this.php_js || {};
      var phpjs = this.php_js;
      var ini = phpjs.ini;
      var obs = phpjs.obs;
      for (i = 0; i < argc; i++) {
        arg = argv[i];
        if (ini && ini['phpjs.echo_embedded_vars']) {
          arg = arg.replace(/(.?)\{?\$(\w*?\}|\w*)/g, replacer);
        }
    
        if (!phpjs.flushing && obs && obs.length) {
          // If flushing we output, but otherwise presence of a buffer means caching output
          obs[obs.length - 1].buffer += arg;
          continue;
        }
    
        if (d.appendChild) {
          if (d.body) {
            if (win.navigator.appName === 'Microsoft Internet Explorer') {
              // We unfortunately cannot use feature detection, since this is an IE bug with cloneNode nodes being appended
              d.body.appendChild(stringToDOM(ieFix(arg)));
            } else {
              var unappendedLeft = stringToDOM(arg, d.body, ns_xhtml, 'div')
                .cloneNode(true); // We will not actually append the div tag (just using for providing XHTML namespace by default)
              if (unappendedLeft) {
                d.body.appendChild(unappendedLeft);
              }
            }
          } else {
            // We will not actually append the description tag (just using for providing XUL namespace by default)
            d.documentElement.appendChild(stringToDOM(arg, d.documentElement, ns_xul, 'description'));
          }
        } else if (d.write) {
          d.write(arg);
        } else {
          console.log(arg);
        }
      }
    }
    
        6
  •  19
  •   Cory R. King    16 年前

    Firebug

    然后,在javascript中:

    var blah = {something: 'hi', another: 'noway'};
    console.debug("Here is blah: %o", blah);
    

    现在您可以查看控制台,单击该语句并查看其中的内容 blah

        7
  •  3
  •   Dallas Clark    12 年前

    这是一个很好的简单解决方案,用于将JSON响应解析为HTML。

    var json_response = jQuery.parseJSON(data);
    html_response += 'JSON Response:<br />';
    
    jQuery.each(json_response, function(k, v) {
        html_response += outputJSONReponse(k, v);
    });
    
    function outputJSONReponse(k, v) {
        var html_response = k + ': ';
    
        if(jQuery.isArray(v) || jQuery.isPlainObject(v)) {
            jQuery.each(v, function(j, w) {
                html_response += outputJSONReponse(j, w);
            });
        } else {
            html_response += v + '<br />';
        }
    
        return html_response;
    }
    
        8
  •  1
  •   Nikola    10 年前

    你也可以试试这个功能。我记不起原作者了,但所有的功劳都归他/她所有。

    就像一个魅力-100%与PHP中的var_dump相同。

    过来看。

    function dump(arr,level) {
    	var dumped_text = "";
    	if(!level) level = 0;
    
    	//The padding given at the beginning of the line.
    	var level_padding = "";
    	for(var j=0;j<level+1;j++) level_padding += "    ";
    
    	if(typeof(arr) == 'object') { //Array/Hashes/Objects
    		for(var item in arr) {
    			var value = arr[item];
    
    			if(typeof(value) == 'object') { //If it is an array,
    				dumped_text += level_padding + "'" + item + "' ...\n";
    				dumped_text += dump(value,level+1);
    			} else {
    				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
    			}
    		}
    	} else { //Stings/Chars/Numbers etc.
    		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    	}
    	return dumped_text;
    }
    
    
    // Example:
    
    var employees = [
        { id: '1', sex: 'm', city: 'Paris' }, 
        { id: '2', sex: 'f', city: 'London' },
        { id: '3', sex: 'f', city: 'New York' },
        { id: '4', sex: 'm', city: 'Moscow' },
        { id: '5', sex: 'm', city: 'Berlin' }
    ]
    
    
    // Open dev console (F12) to see results:
    
    console.log(dump(employees));
        9
  •  0
  •   mike rodent    7 年前

    我提出这一点是为了帮助任何需要一些实用的东西的人,为你提供一个漂亮的、经过修饰的(缩进的)JS图片 Node . 其他的解决方案对我来说都不管用 节点 (“周期性错误”或其他…)。这将引导您通过DOM下的树 tagName (如适用)以及 textContent (如适用)。

    您在头节点下行走时遇到的节点的任何其他细节都可以根据您的兴趣添加。。。

    function printRNode( node ){
        // make sort of human-readable picture of the node... a bit like PHP print_r
    
        if( node === undefined || node === null ){
            throwError( 'node was ' + typeof node );
        }
        let s = '';
    
        // NB walkDOM could be made into a utility function which you could 
        // call with one or more callback functions as parameters...
    
        function walkDOM( headNode ){
          const stack = [ headNode ];
          const depthCountDowns = [ 1 ];
          while (stack.length > 0) {
            const node = stack.pop();
            const depth = depthCountDowns.length - 1;
            // TODO non-text, non-BR nodes could show more details (attributes, properties, etc.)
            const stringRep = node.nodeType === 3? 'TEXT: |' + node.nodeValue + '|' : 'tag: ' + node.tagName;
            s += '  '.repeat( depth ) + stringRep + '\n';
            const lastIndex = depthCountDowns.length - 1;
            depthCountDowns[ lastIndex ] = depthCountDowns[ lastIndex ] - 1;
            if( node.childNodes.length ){
                depthCountDowns.push( node.childNodes.length );
                stack.push( ... Array.from( node.childNodes ).reverse() );
            }
            while( depthCountDowns[ depthCountDowns.length - 1 ] === 0 ){
                depthCountDowns.splice( -1 );
            }
          }
        } 
        walkDOM( node );
        return s;
    }