Saturday, January 12, 2013

Snippets in JS

Mainly useful to me, but can also be found useful by others, I'll put here some of the JS snippets I don't want to loose. Some are written by me, some may be found on the internet in which case I'll try my best to give credit to the author(s). This document should be a living document. Feel free to comment and submit new snippets in your comments. Cheers.


Snippet #1 [Browser]

/*
  Get the JSON params of the current script
  
  Example markup:

    <script src="path/to/script.js#{a:1,b:2}"></script>

    <script>
      var params = getScriptJSONParams("path/to/script.js");
      // => {a:1, b:2}
    <script>
    
*/

function getScriptJSONParams(script_name_regexp, doc) {
  var scripts = doc.getElementsByTagName("script")
  , n = scripts.length;
  
  if (typeof script_name_regexp === 'string') script_name_regexp = new RegExp(script_name_regexp);
  
  while (n--) {
    var script = scripts[n]
    if (script_name_regexp.test(script.src)) return JSON.parse(script.src.split('#')[1]);
  }
}

Snippet #2 [Browser] found on the net (don't remember where :/)

/* 
  JSON _parse_ when "JSON" lib is not available in a browser
*/

function evil(str) { return eval("(" + str + ")") }

Snippet #3 [Browser|Nodejs]

/* 
  My personal commented interpretation of John Resig's class for javascript

  Highly based on:
    Simple JavaScript Inheritance By John Resig http://ejohn.org/
    MIT Licensed.
    http://ejohn.org/blog/simple-javascript-inheritance/

  Example usage:

  var Foo = Class.extend({ init: function(n) { this.foo = n }, hello: function(){ return "Hello World!" } });

  var Bar = Foo.extend({ init: function () {
    this.bar = "Bar";
    Bar.Super(this, 'init', 4); // It is a call to the Super-prototype, and is a shortcut for:
                                // Foo.prototype.init.call(this, 4)
  }});

  console.log(new Bar);
  // { bar: 'Bar', foo: 4 }

  console.log((new Bar).hello());
  // Hello World!

*/

!function(name, def) {
  if (typeof define === 'function') define(def);
  else if (typeof module !== 'undefined') module.exports = def();
  else this[name] = def()
}('Class', function () {
  var shouldInit = false; // Exterior to the Class.Extend because it needs to be controlled for Every Class object

  // The base Class implementation (does nothing)
  var Class = function(){};

  // Create a new Class that inherits from this class
  Class.extend = function(prop) { 
    var _super = this.prototype; // The Super prototype
    
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    shouldInit = false;
    var prototype = new this(); // The new prototype Object
    shouldInit = true;
    
    // Copy the properties over onto the new prototype
    for (var name in prop) { prototype[name] = prop[name]; } // Feeding the new prototype Object with the argument's Object
    
    // The dummy class constructor
    function Klass() {
      // All construction is actually done in the init method
      if (shouldInit && this.init) this.init.apply(this, arguments);
    }
    
    // Populate our constructed prototype object
    Klass.prototype = prototype;
    
    // Create a tool Super() function to access the Super prototype
    Klass.Super = function (self, prop) { 
      if (arguments.length < 2) return _super;
      return (typeof _super[prop] === 'function') ? _super[prop].apply(self, [].slice.call(arguments, 2)) : _super[prop];
    }
    
    // Enforce the constructor to be what we expect
    Klass.prototype.constructor = Klass;

    // And make this class extendable
    Klass.extend = arguments.callee;
    
    return Klass;
  };

  return Class;
});


No comments:

Post a Comment