2

I want to set a global variable via a set function but it always sets it to undefined value:

var name;
var setName = function(name){
    this.name = name;
}
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168
daisy
  • 615
  • 2
  • 8
  • 15
  • 1
    `window.name` is a predefined property which might not be writable. Either use a different variable name or better, avoid global variables. Additionally, whether `this.name` works depends on how you are calling `setName`. – Felix Kling May 18 '13 at 15:30
  • Are you actually passing a variable into the function, or expecting that the function will automagically populate the `name` parameter/argument with the `name` variable defined previously? – David Thomas May 18 '13 at 15:31

3 Answers3

3

In this case you don't use this. (It could work but not necessarily. Why? Read this)

Just use:

var name;
var setName = function (aname) {
    name = aname;
}

Also make sure this code is not inside of another scope (function):

function foo() {
    // ...
    var name;
    var setName = function (aname) {
        name = aname;
    }
    // ...
}

In this case name would not be global and you would have to omit its declaration to make it global:

function foo() {
    // ...
    var setName = function (aname) {
        name = aname; // not it is global, if not defined in the visible scope
    }
    // ...
}

But this is a bad practice, so try to avoid polluting the global namespace. A variable is considered in the global namespace if:

  • it has no var (name = ..., and there is no other visible local variable called name
  • it is set on window (e.g. window.name = ...)
Community
  • 1
  • 1
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168
  • thanks, I used global to express that it is outside the set function, I come from Java. I normally used this.name = name to set an intance variable in Java. – daisy May 18 '13 at 15:41
  • Despite the fact that javascript has 'java' at the beginning, javascript has nothing to do with java. Not one thing. – 7stud May 18 '13 at 21:10
1
function(val){

    name = val

.......................

7stud
  • 46,922
  • 14
  • 101
  • 127
1

First, window.name is a reserved property, and you should not use it.

Second, it's a good idea to namespace any global JavaScript, to avoid those types of issues or conflicts with third party libraries.

Here's a working example:

window.MyNamespace = {};
MyNamespace.name = '';
MyNamespace.setName = function(value) {
    MyNamespace.name = value;
};
MyNamespace.setName('hello');
MyNamespace.name; // "hello"

Based on your comment about this being in a module, this code will work:

(function(){ 
    app = {};  
    app.myPerson = (function(){ 
        var name;
        var setName = function(value) {
            name = value;
        };      
        setName('Charlie');
        console.log(name); // Charlie
    })();        
})(); 
Craig
  • 691
  • 4
  • 20
  • sorry I used global to express that it is outside the set function, I come from Java. I normally used this.name = name to set an intance variable in Java. – daisy May 18 '13 at 15:40
  • Is your setName method supposed to be defined within a JavaScript object? – Craig May 18 '13 at 15:45
  • it is inside a module: (function(){ app.myPerson = (function(){ var name; })(); })(); – daisy May 18 '13 at 15:49
  • Modules are intended to be self contained. If you need to access app.myPerson.name, you need to change myPerson from a module to an object. – Craig May 18 '13 at 16:22