-1

it is possible to set value for the local variable when you call it ? it is my code

var myvar={
F:function(){
    var x;
    return x;}
 }

my problem is when I call the x variable and set value like myvar.F().x="hello"; . then recall it console.log(myvar.F().x) x don't contain that value only return undefined ? how i can fix it but please don't suggest me to set the value inside F function AND Using Parameters from F function ! other way because I want make a dynamic variable not static value Thanks for help

Ega
  • 55
  • 19
  • The simple answer is, you can't set the `x` outside the function by any means. – Antti Haapala -- Слава Україні Feb 08 '15 at 10:09
  • `myvar.F()` is a function, not an object, that returns a 1-time value (of null if I remember JS correctly). – Jhecht Feb 08 '15 at 10:09
  • You could just pass the value of x as an argument, see http://jsfiddle.net/vfapp42s/ – Sven Feb 08 '15 at 10:09
  • 2
    @Jhecht—you don't remember correctly. The OP's function returns the value of *x* (which happens to be undefined). A function with no return statement, or no explicit return value, returns *undefined*. – RobG Feb 08 '15 at 10:11
  • Ah damn - can't remember 'em all I guess! – Jhecht Feb 08 '15 at 10:13
  • @Jhecht is correct when he says myvar.F is not an object – Charlie Feb 08 '15 at 10:15
  • I really don't see the point of what are you doing. Using stack never hurt anyone and I think this is the case here. Add x to parameters of the function F and then call it. I don't know why you want to do this so complicated. If you insist, please tell us the reason. – jPO Feb 08 '15 at 10:27
  • Well as you just added the edit with the AND Using Parameters from F function I'm giving you downvote, because this really is quite inappropriate question. You should try joining hogwarts, it'd be much more plausible then what you want to achieve. – jPO Feb 08 '15 at 10:29
  • 1
    I want set value to X without using Parameter what is wrong ? – Ega Feb 08 '15 at 10:32
  • @ega: if you want to set x without function parameter... then why F is a function and not a variable? – kasper Taeymans Feb 08 '15 at 10:45
  • @Charlie, everything in JavaScript is an object, so are functions. You can extend the function object by `Function.prototype.x`. – Mouser Feb 08 '15 at 10:59
  • @Mouser I'd Hope you know what we meant... No need to reiterate the most basic of technicalities. look at `myvar.F().x` he is clearly calling the function not trying to access it's prototype. The problem **is** illuminated by pointing out that he is trying to treat a function like an object... Or would you prefer a function Object like an Object... – Charlie Feb 08 '15 at 18:31

4 Answers4

1

Despite the answers here, some comments are very clear. The thing you want is not possible.

You can set a variable to the return of a function, but I won't be saved anywhere. The reference is immediately lost.

    var myvar = {
      f: function() {
        var x = 'something';
        return x;
      }
    }
    document.write(myvar.f());

This will return x from the method f inside the object myvar. The object returned is a string. In your original code x isn't event defined as something. Since everything in JavaScript is an object, you can assign a property x to the result if it's defined, BUT that is not the case here:

    var myvar = {
      f: function() {
        var x;
        return x;
      }
    }
    
    var a = myvar.f();
    
    document.write(a + "<br />");
    document.write(Object.prototype.toString.call(a) + "<br />"); //returns [object Undefined]

So you're attaching a property to a undefined which never gets set. To be able to set x you need to make it a property of the function myvar. Setting it to the method will only work if you don't invoke the method. Invoking the method causes it to return the variable that is not bound to the method object itself.

Mouser
  • 13,132
  • 3
  • 28
  • 54
1

If I understand correctly, you want the result of the function call to be a stateful object that you can set and fetch values from.

I'm also assuming you don't want the object which is returned from the function to be accessible anywhere else in the parent object.

In order to do this, you'll need to change the way your object is created.

var Factory = function () { 
    var _priv = { 
        x: "something" 
    }; 

    return { 
        F: function () { 
            return _priv; 
        } 
    }; 
};

var myvar = new Factory();

myvar.F().x; // "something"
myvar.F().x = "hello world";  // "hello world"

So, the creation of the initial object is a bit different, but you have the desired results.

If you don't mind that the returned objects properties are accessible elsewhere on the parent object, you could do this:

var myvar = {
    _priv: {
        x: "something"
    },
    F: function () {
        return this._priv;
    }
};

myvar.F().x; // "something"
myvar.F().x = "hello world";  // "hello world"

The downside being that the _priv property is also accessible on the main object:

myvar._priv.x; // "hello world"

Does this answer your question?

Nick Jennings
  • 3,853
  • 6
  • 30
  • 45
0
var myvar = {
  F: function(x) {
    if (x) {
        this.x = x; // if you are setting the value of x
    }
    else {
        return this.x; // if you want the value of x
    }
  }
}

myvar.F("foo"); // Setter
myvar.F(); // "foo" (Getter)
Charlie
  • 376
  • 1
  • 10
-1

(You'll need to open up the console on your browser) does this help to mimic the behavior you want? I'm not 100% on why you would want to do something like this to begin with.

var myvar = {
  f: function() {
    this.x = this.x ||'';
    return this;
  }
}

console.log(myvar.f());
myvar.f() .x = "Not something";
console.log(myvar.f.x);
console.log(myvar.f().x);
Jhecht
  • 4,407
  • 1
  • 26
  • 44
  • no I want set the value to myvar.f().x not to myvar.f.x it is variable you are defined not a part of my code – Ega Feb 08 '15 at 10:20
  • As has been said, what you want exactly cannot be done. Does my edit get closer? Why exactly do you *have* to have it this format? – Jhecht Feb 09 '15 at 03:22