如何在Javascript中使对象全局化?

时间:2014-06-24 12:06:38

标签: javascript android cordova

我制作了一个小的phonegap / cordova应用程序,我需要访问我的.js文件中的一个对象。结果我不知道如何改变我的代码结构来实现这一点。

这是我的index.html代码:

<!DOCTYPE html>
<html>
    <head>
        <title>NFC tag ID reader</title>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript" charset="utf-8">
            // Wait for device API libraries to load
            document.addEventListener("deviceready", onDeviceReady, false);

            // device APIs are available
            function onDeviceReady() {
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
            }
            function gotFS(fileSystem) {
                fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
            }
            function gotFileEntry(fileEntry) {
                fileEntry.createWriter(gotFileWriter, fail);
            }

            function gotFileWriter(writer) {
                writer.onwriteend = function(evt) {
                    console.log("contents of file now 'some sample text'");
                    writer.truncate(11);
                    writer.onwriteend = function(evt) {
                        console.log("contents of file now 'some sample'");
                        writer.seek(4);
                        writer.write(" different text");
                        writer.onwriteend = function(evt){
                            console.log("contents of file now 'some different text'");
                        }
                    };
                };
                writer.write("some sample text");
                //MAKE THIS OBJECT GLOBAL ?
            }

            function fail(error) {
                console.log(error.code);
            }
        </script>
    </head>
    <body>
        <div class="app">
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>

这是我的index.js代码:

var app = {
    /* Application constructor */
    initialize: function() {
        this.bindEvents();
        console.log("Starting NFC Reader app");
    },
    /* bind any events that are required on startup to listeners: */
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    /* this runs when the device is ready for user interaction: */
    onDeviceReady: function() {
        nfc.addTagDiscoveredListener(
            app.onNfc,             // tag successfully scanned
            function (status) {    // listener successfully initialized
                app.displayCpt("<b>"+cpt+"</b>" + ' personnes restantes.');
                app.displayBjr("\n");
                app.displayBjr("Identifiez-vous:");
            },
            function (error) {     // listener fails to initialize
                app.display("NFC reader failed to initialize " +
                JSON.stringify(error));
            }
        );
    },
    /* displays tag ID from @nfcEvent in message div: */

    onNfc: function(nfcEvent) {
    var tag = nfcEvent.tag;
    var nfcUid = nfc.bytesToHexString(tag.id);
    var myDb = {
        "04c85ccab52880": {
            "name": "name",
            "firstname": "fname",
            "societe": "work"
        }
        var mapped = Object.keys(myDb).map(function(uid){
            return (myDb[uid].uid = uid) && myDb[uid];
        });

        for(var i = 0; i < mapped.length ; i++){
            if(mapped[i]['uid'] != nfcUid){
                mapped[i]['uid'] += 1;
            } else {
                mapped[i]['uid'] = nfcUid;
                app.display(mapped[i]['name'] + ' ' + mapped[i]['firstname'] + ', ' + mapped[i]['societe']);
                writer.write(mapped[i]['name'] + ' ' + mapped[i]['firstname'] + ', ' + mapped[i]['societe']);
                //I WOULD NEED THIS WRITER USABLE IN ORDER TO WRITE MY ARRAY CONTENT INTO A FILE
            }
        }

    },

};     // end of app

我认为我的writer对象需要是全局的才能使映射的数组写入文件,但是我找不到这样做的方法......有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

您是否尝试将var writer置于所有内容之外并将其从gotFileWriter(writer)的参数列表中删除?这应该使它成为一个全球变量。请注意,这不是最好的编程实践,应该尽可能避免全局变量。