By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,608 Members | 1,940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,608 IT Pros & Developers. It's quick & easy.

Changing and value of variable in function

P: 22
How can i change the value of a variant that defined in a function for ever?

Expand|Select|Wrap|Line Numbers
  1. function test()
  2. {
  3. this.data = 'hello';
  4. }
  5.  
  6. function change()
  7. {
  8. var a = new test();
  9. a.data = 'hi';
  10. alert(a.data);
  11. }
  12.  
  13. change(); // return hi
  14.  
  15. var b = new test();
  16. alert(b.data); // return hello
  17.  
Is any solution for return the new value of 'data' variable that replaced using change() function.
Aug 30 '09 #1
Share this Question
Share on Google+
3 Replies


hsriat
Expert 100+
P: 1,654
Expand|Select|Wrap|Line Numbers
  1. function test() //<<== is a class
  2. {
  3. this.data = 'hello';
  4. }
  5.  
  6. function change()
  7. {
  8. var a = new test(); //<<== is object of test class
  9. a.data = 'hi'; // you change data variable of 'a' object but not 'test' class.
  10. alert(a.data);
  11. }
  12.  
  13. change(); // return hi //of course!
  14.  
  15. var b = new test(); //another object... not the last object a
  16. alert(b.data); // return hello //of course... you didn't change this 'b' object
  17.  
Solution:
Make the function within that test function

Expand|Select|Wrap|Line Numbers
  1. function test() {
  2.     this.data = "hello";
  3.     this.change = function() {
  4.         this.data = "hi";
  5.     }
  6. }
  7.  
  8. var a = new test();
  9. alert(a.data); //will return hello
  10. a.change();
  11. alert(a.data); //will return hi
Aug 30 '09 #2

P: 22
Thanks

But is it possible to return changed value (hi) using another object defining ('b' object), but not using 'a' object?
Something like last line.

Expand|Select|Wrap|Line Numbers
  1. function test()
  2. {
  3. this.data = 'hello';
  4. this.change = function()
  5. {
  6. this.data = 'hi';
  7. }
  8. }
  9.  
  10. var a = new test();
  11. alert(a.data); // will return hello
  12. a.change();
  13. alert(a.data); // will return hi
  14.  
  15. var b = new test(); // or 'var b = test();' but its wrong
  16. alert(b.data); // return hello (hi?)
Aug 31 '09 #3

gits
Expert Mod 5K+
P: 5,390
basicly you would need to call the change method to get 'hi' returned or create a derived obj like this:

Expand|Select|Wrap|Line Numbers
  1. function test() {
  2.     this.data = 'hello';
  3.     this.change = function() {
  4.         this.data = 'hi';
  5.     }
  6. }
  7.  
  8. var a = new test();
  9. alert(a.data); // will return hello
  10. a.change();
  11. alert(a.data); // will return hi
  12.  
  13. function myTest() {
  14.     this.change();
  15. }
  16.  
  17. myTest.prototype = new test();
  18.  
  19. var b = new myTest();
  20. alert(b.data);
  21.  
kind regards
Aug 31 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.