2013年8月5日 星期一

New local JS file which provides functions for external JS files to use

 JS file:

function atv() {}









atv.prototype = {
    ProxyDocument: function() {
        this.show = function() {};
        this.onCancel = function() {};
        this.loadXML = function(xmldoc, handler) {
            xmlText = new XMLSerializer().serializeToString(xmldoc);
            gtkTV.loadXML(gtkTV.parseXML(xmlText));
        };
    },
    loadXML: function(doc) {
        gtkTV.loadXML(doc);
    },
    parseXML: function(xml) {
        return gtkTV.parseXML(xml);
    },
    loadPlist: function(item) {      
        gtkTV.sponsor_play(item['media-asset']['media-url']);
    },
    onAppEntry: function() {},
    onAppExit: function() {},
    config: 0,
    localStorage: function() {
        this.getItem = function(key) {
            return this[key];
        };
        this.setItem = function(key, value) {
            this[key] = value;
        };
        this.SHOWTIMES_AVAIALABLE = 1;
    },
    sessionStorage: function() {
        this.getItem = function(key) {
            return this[key];
        };
        this.setItem = function(key, value) {
            this[key] = value;
        };
        this.DEBUG_LEVEL = 0;
    },
    player: function() {
        this.currentAssetChanged = 0;
        this.playerStateChanged = 0;
        this.willStartPlaying = 0;
        this.playerTimeDidChange = 0;
    },
    device: function() {
        this.udid = 'DY3HJY89DRHN';
        this.displayName = 'Gemtek';
    },
    Document: function() {},
    Element: function() {}
}

atv = new atv;
atv.sessionStorage = new atv.sessionStorage;
atv.localStorage = new atv.localStorage;


Javascript has no actual class identifier. We use function to achieve the similiar idea. In the code, atv.prototype defines the methods in the atv function. Javascript uses function to define a instance, and for the special use of declaration like "atv.sessionStorage", a nested function prototype needs to be defined within atv function. In order to use them, create them using new and it looks like same naming does not violate much of the Javascript semantics.

Reference links
http://blog.roodo.com/rocksaying/archives/18294075.html
http://repl.it/
http://bonsaiden.github.io/JavaScript-Garden/zhtw/#function.constructors
http://stackoverflow.com/questions/9898698/convert-xml-to-string-and-append-to-page

沒有留言:

張貼留言