Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit</strong>: added pretty-printing.</p> <p>You could <code>JSON.stringify()</code> the argument of the constructor, if it is JSON-compatible. Here is a <code>toString()</code> function that builds on this idea, but with a slightly generalized version of <code>JSON.stringify()</code> that accepts stringifying functions:</p> <pre><code>function Shape(x){ this.x = x; } Shape.prototype.toString = function() { function stringify(data, prefix) { function unicode_escape(c) { var s = c.charCodeAt(0).toString(16); while (s.length &lt; 4) s = "0" + s; return "\\u" + s; } if (!prefix) prefix = ""; switch (typeof data) { case "object": // object, array or null if (data == null) return "null"; var i, pieces = [], before, after; var indent = prefix + " "; if (data instanceof Array) { for (i = 0; i &lt; data.length; i++) pieces.push(stringify(data[i], indent)); before = "[\n"; after = "]"; } else { for (i in data) pieces.push(i + ": " + stringify(data[i], indent)); before = "{\n"; after = "}"; } return before + indent + pieces.join(",\n" + indent) + "\n" + prefix + after; case "string": data = data.replace(/\\/g, "\\\\").replace(/"/g, '\\"') .replace(/\n/g, "\\n").replace(/\r/g, "\\r") .replace(/\t/g, "\\t") .replace(/[\x00-\x19]/g, unicode_escape); return '"' + data + '"'; default: return String(data).replace(/\n/g, "\n" + prefix); } } return "new Shape(" + stringify(this.x) + ")"; }; var rect = new Shape({color : 'rgba(0,0,0,1)' , x : 0 , y : 0 , w : 100 , h : 100 , draw : function() {ctx.fillStyle = this.color; ctx.fillRect(this.x,this.y,this.w,this.h); } }); console.log(rect.toString()); </code></pre> <p>The output is:</p> <pre><code>new Shape({ color: "rgba(0,0,0,1)", x: 0, y: 0, w: 100, h: 100, draw: function() { ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, this.w, this.h); } }) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload