Javascript notes

console usage examples

var MyView = function() {

    this.initialize = function() {
        console.log('MyView.initialize()');
        this.el = $('<div>MyView el</div>');
        console.log("MyView.el: %o", this.el);
    };

    // ...
    // ...
    // ...

    this.initialize();
}

Stack trace:

console.log("%s", new Error().stack);

In Firefox you can also use:

console.trace();

See also:

Console (Mozilla Developer Network)

assert in javascript

function AssertException(message) { this.message = message; }
AssertException.prototype.toString = function () {
  return 'AssertException: ' + this.message;
}

function assert(exp, message) {
  if (!exp) {
    throw new AssertException(message);
  }
}

credits: http://aymanh.com/9-javascript-tips-you-may-not-know

Images pre-fetch

    function prefetchImages() {
        images = [
            '{{ MEDIA_URL }}css/jplayer/mute-hover.png',
            '{{ MEDIA_URL }}css/jplayer/unmute-hover.png',
            '{{ MEDIA_URL }}css/jplayer/volume-max-hover.png',
            '{{ MEDIA_URL }}css/jplayer/play-hover.png',
            '{{ MEDIA_URL }}css/jplayer/pause-hover.png',
            '{{ MEDIA_URL }}css/jplayer/stop-hover.png',
            '{{ MEDIA_URL }}css/jplayer/previous-hover.png',
            '{{ MEDIA_URL }}css/jplayer/next-hover.png'
        ];

        for ( var i=0; i<images.length; i++ ) {
            var img = new Image();
            img.src = images[i];
        }
    }