title: Node.js: Extend JSON object
body: <p>I would like to extend the native <code>JSON</code> object of node.js. In my example, I was able to extend it to add a <code>merge(json1, json2)</code> function, which merges 2 JSONs:</p>
  
  <pre><code>JSON.merge = function(target) {
      var sources = [].slice.call(arguments, 1);
      sources.forEach(function (source) {
          for (var prop in source) {
              target[prop] = source[prop];
          }
      });
      return target;
  }
  </code></pre>
  
  <p>This script is larglely inspired from this question: <a href="https://stackoverflow.com/questions/14974864/combine-or-merge-json-on-node-js-without-jquery">Combine or merge JSON on node.js without jQuery</a></p>
  
  <p>To have a cleaner code, I put this script in a library folder, and require it in my main script. This is what I did...</p>
  
  <p>My <code>lib/JSON.js</code> file:</p>
  
  <pre><code>extends.merge = function(target){
      // The same script as above.
  }
  </code></pre>
  
  <p>and in my <code>app.js</code> file:</p>
  
  <pre><code>JSON = require('./lib/JSON')
  </code></pre>
  
  <p>It works well, but now, native JSON methods don't work (like <code>.stringify</code>). Then I digged a little bit, and tried to extend JSON using <code>.prototype</code>, like this (in <code>lib/JSON.js</code>):</p>
  
  <pre><code>extends.prototype.merge = ...
  </code></pre>
  
  <p>But get the following error:</p>
  
  <pre><code>TypeError: Cannot set property 'merge' of undefined
  </code></pre>
  
  <p>I really don't know what to do (and what I'm doing) since solving a problems causes another problem.</p>
  
  <p>Please, need help.</p>
  
  <p>Thanks.</p>