codingdir logo sitemap sitemap |

Storing variables in my name spaced javascript

By : , Category : javascript
(function() {
    var id = 0, var1; //DECLARE VARIABLE HERE = function() {
        return id++;  

    // Store variables here ...
    this.variableStore = function() {
            this.var1 = "One"; //NO "VAR" KEYWORD

Using var var1 = "One"; creates var1 in the local scope, so you can't access it from the instance of myApp. Also, remember to use this.var1; otherwise the variable var1 is essentially a private variable and can't be accessed from the outside.

Also, if you want to use


Then you'll have to return myApp; in your variableStore method. This is because myApp.variableStore() currently returns nothing, so you can't access var1 of nothing. So, here is the complete code:

var myApp = {};
(function() {
    var id = 0, var1; = function() {
        return id++;  

    // Store variables here ...
    this.variableStore = function() {
            this.var1 = "One";
            return myApp;
ReLated :

the problem is not the url var issue, the logic to generate parentSrvObjRefW1 or tat_Name is incorrect. This is making those variables undefined. Or you are not initialising the parameter.

DOM Elements, by their own nature are global.

Becuase your document is a global object they are accessible from anywhere in the app anyway.

What you're doing is good in a way that it facilitates the reuse of code but it doesn't solve any of the problems of global scope - meaning, your DOM elements are any accessible throughout the app whether you employ this strategy or not. :)

Another problem however, is that if you store reference to the same DOM object in three different variables for example, and you alter one of those three variables. This will result in DOM manipulation for remaining two as well. Like any other language/technology, this is not the problem of technology itself. It is the problem caused by not following good coding ethos. :)

It is easy:

 var response_from_server = null;// variable for storage the response

 // POST for user object 
    $.post("", {
        client_id: clientID,
        client_secret: clientSecret,
        grant_type : 'authorization_code',
        redirect_uri: redirectURI,
        code: user_code},
  function(data) {
    response_from_server = data; // store the response


// Step 1:
var response_from_server = null;// variable is defined
// there response_from_server is null
// Step 2:
// Step 4:
response_from_server = data; // store the response
// there response_from_server is data, because the POST is finished
// Step 3:
// there response_from_server is still null, because POST call in process

I've used this in the past to read a txt-file:

JavaScript (jQuery):

function readFile() {
    $.post('path/to/php/read_file.php', {
        dir: 'path/to/file',
        file: 'file_name.txt',
    },function(file_contents) {
        if (file_contents != '') {
            //your code here


    /* This script is invoked by 'index.html'.
     * This script gets the content of the file that has been changed by 
     * the admin, and uses it to determine the path of the file 
     * that is currently set by the admin.
    $dir = $_POST["dir"];
    $file = $_POST["file"];
    $contents= '';

    if ($dhandle = opendir("$dir")) {
        while (false !== ($entry = readdir($dhandle))) {
            if ($entry != "." && $entry != "..") {
                if ($entry == $file) {
                    $entry_path = $dir.DIRECTORY_SEPARATOR.$entry;

                    $fhandle = fopen($entry_path, 'r');
                    $value = fread($fhandle, filesize($entry_path));

        echo "$contents";

Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .