0

I have never wrote javascript on the below syntax. What I want to do is simply declare a variable outside the function "showUrlLink", what is the correct way to do it?

//File test.js
    var Test =
                {
                    //Declare variable here to be used in showUrlLink
                    var test = "something";//This gives syntax error
                    showUrlLink: function() {
                        if (test === "something") {
                            //Do something
                        }
                    }
                };

Thanks in advance.

Huangism
  • 16,278
  • 7
  • 48
  • 74
anders
  • 1,535
  • 5
  • 19
  • 43
  • Add the property `test : "something"` instead of `var test = "something";` Also use `this.test` instead of `test`. – Earth Jul 30 '14 at 12:23

4 Answers4

1

If you want to declare some internal variables in the scope of Test object, but only return a "public interface" you should wrap it in a function:

var Test = (function(){
    //Declare variable here to be used in showUrlLink
    var test = "something";
    return {
        showUrlLink: function() {
            if (test === "something") {
                console.log( test );
            }
        }
    }
}());

Now you can do Test.showUrlLink(), but you can not change Test.test variable because it's kept in the closure and not accessible from the outside scope.

http://jsfiddle.net/K3P25/

See also: http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

pawel
  • 35,827
  • 7
  • 56
  • 53
  • This is the cleanest approach out of the current answers, though probably the most difficult to understand for a novice. – I-Lin Kuo Jul 30 '14 at 15:03
  • Thanks, that's why I have added the link at the end - maybe someone will learn something new :) – pawel Jul 30 '14 at 15:08
0

Just add as a property to the object

 var Test ={
                   test : "something",
                    showUrlLink: function() {
                        if (this.test === "something") {
                            //Do something
                        }
                    }
                };
vimal1083
  • 8,499
  • 6
  • 34
  • 50
0

It's a literal, so you could use a property instead or just declare the variable before the Test variable. You can't declare variables in a literal where properties are expected.

var Test = {
     test : "something",
     showUrlLink: function() {
         if ( this.test === "something") {
            //Do something
         }
     }
};
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

It depends how you wan't to make it / use it.

Easiest fix (but maybe not the best, because you should avoid defining global variables) is to move your var test = "something"; declaration outside the Object declaration:

// Defined before the Object below, so it works correctly
var test = "something";

var Test = {
    showUrlLink: function () {
        if (test === "something") {
            //Do something
            console.log("test is: " + test);
        }
    }
};

Test.showUrlLink();     // test is: something

Alternatively, you could define test to be a property of an Object itself. See example below.

Using a JavaScript Object:

var Test = {
    test: "something",
    showUrlLink: function () {
        if (this.test === "something") {
            //Do something
            console.log("this.test is: " + this.test);
        }
    }
};

Test.showUrlLink();     // this.test is: something

Finally, you could use a JavaScript Class instead of Object.

Using a JavaScript Class:

/**
 * Class definition
 *
 */
var Test = function () {
    this.test = "something";
};

/**
 * Method for showing showUrlLink
 *
 */
Test.prototype.showUrlLink = function () {
    if (this.test === "something") {
        //Do something
        console.log("this.test is: " + this.test);
    }
};

// How to use the Class
var test1 = new Test();
test1.showUrlLink(); // this.test is: something

var test2 = new Test();
test2.showUrlLink(); // this.test is: something

All in all, I usually prefer Classes over Objects, but only if I find myself defining multiple functions inside an Object. Moreover, Classes lets you create multiple instances.

Finally, JavaScript doesn't actually have "classes" but people (like me) use to refer them as Classes instead of constructor functions. More about it in this link.

Cheers.

Community
  • 1
  • 1
Mauno Vähä
  • 9,688
  • 3
  • 33
  • 54