diff --git a/packages/base.js b/packages/base.js index 9d1fc96..848b4a5 100644 --- a/packages/base.js +++ b/packages/base.js @@ -1,6 +1,59 @@ +/** + * base.js + * This file contains all global functions provided by js.io. + */ + exports.log = jsio.__env.log; exports.GLOBAL = jsio.__env.global; +/** + * Various polyfill methods to ensure js.io implementations provide + * a baseline of JavaScript functionality. Feature compatibility (localStorage, + * etc.) should be provided elsewhere. + */ + +// Array.isArray +// Not available before ECMAScript 5. +// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray + +if (!Array.isArray) { + Array.isArray = function (arg) { + return Object.prototype.toString.call(arg) === '[object Array]'; + } +}; + +// Function.prototype.bind +// Not available before ECMAScript 5. +// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind + +if (!Function.prototype.bind) { + Function.prototype.bind = function (oThis) { + if (typeof this !== "function") { + // closest thing possible to the ECMAScript 5 internal IsCallable function + throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function () {}, + fBound = function () { + return fToBind.apply(this instanceof fNOP + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + fNOP.prototype = this.prototype; + fBound.prototype = new fNOP(); + + return fBound; + }; +} + +/** + * DEPRECATED. Old js.io polyfills. + */ + var SLICE = Array.prototype.slice; /* Use native isArray if available @@ -38,6 +91,10 @@ exports.bind = function(context, method /*, VARGS*/) { } } +/** + * Class constructor. + */ + exports.Class = function(name, parent, proto) { return exports.__class__(function() { return this.init && this.init.apply(this, arguments); }, name, parent, proto); } @@ -100,6 +157,10 @@ var ErrorParentClass = exports.__class__(function ErrorCls() { } }, function() {}); +/** + * Merge two objects together. + */ + exports.Class.defaults = exports.merge = function(base, extra) { base = base || {}; @@ -116,6 +177,10 @@ exports.merge = function(base, extra) { return base; } +/** + * Create a timer delay. + */ + exports.delay = function(orig, timeout) { var _timer = null; var ctx, args; @@ -128,7 +193,10 @@ exports.delay = function(orig, timeout) { } } -// keep logging local variables out of other closures in this file! +/** + * Log constructor and default "logger". + */ + exports.logging = (function() { // logging namespace, this is what is exported @@ -187,4 +255,4 @@ exports.logging = (function() { return logging; })(); -var logger = exports.logging.get('jsiocore'); +var logger = exports.logging.get('jsiocore'); \ No newline at end of file