0

Testing my ability to write code in JavaScript and Node (perhaps a bit of a monumental effort) and also attempting to understand standards.

I want to dynamically change an attribute in an object as in this examnple:

    var parms = {
        host:'',
        port:'',
        user:'',
        pass:''
    };
    
    parms.user='foo';
    parms.pass='bar';
    console.log(parms.user);
    setParm = function(param,value){
        parms.param = value;
    }
    
    setParm('user','baz');
    console.log(parms.user);

However, I'm completely blind. I feel as though I may be in a blind alley in terms of what I think is possible versus what is actually workable.

11
  • 2
    I think you need parms[param] = value; Commented Jul 29, 2017 at 0:17
  • Ahhhhhh! Yes. That was it. Fundamentals. Commented Jul 29, 2017 at 0:19
  • I'm a basic learner myself. But I'm glad I could help :) Commented Jul 29, 2017 at 0:20
  • See MDN Property accessors. Commented Jul 29, 2017 at 0:31
  • 1
    @kenIngram: you seem to want to learn the fundamentals, so although not explicitly asked let me recommend these online and free books. eloquentjavascript.net (beginner) , github.com/getify/You-Dont-Know-JS (intermediate / advanced) . Takes a couple of weeks but this is (arguably) the best stuff that's out there. Good luck! Commented Jul 29, 2017 at 0:48

1 Answer 1

1

You are passing the property as a string, so accessing with . won't work. One solution I know is that you can use dict-like indexing:

    var parms = {
        host:'',
        port:'',
        user:'',
        pass:''
    };
    
    parms.user='foo';
    parms.pass='bar';
    console.log(parms.user);
    setParm = function(param,value){
        parms[param] = value;
    }
    
    setParm('user','baz');
    console.log(parms.user);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.