4

I have a var in ES5 code which looks like below.

  var options = {
  clientId : clientId
  keepAlive : keepAlive,
  clean : clean,
  reconnectPeriod : reconnectPeriod,
  will : lastWillMessage
};

I could change this to ES6 by saying

let options = {clientId,keepAlive,reconnectPeriod,lastWillMessage};

But how do I do this if I don't want to pass values for all the properties? For instance I just want to pass the clientId and the keepAlive. Rest will have the default values set by MQTT module. So how can I set values for only certain properties of this object?

7
  • Your ES6 version omits clean, and also renames lastWillMessage. Commented May 13, 2016 at 3:33
  • 2
    I don't think I understand the question...if the MQTT module will set default values for absent parameters, then can't you just leave them out? Commented May 13, 2016 at 3:39
  • 1
    You have not specified clean in the ES6 version, so of course it will be omitted. Commented May 13, 2016 at 3:39
  • 1
    "It" does not "automatically" do anything. "It" just does what you say. If you tell it to put clean in the ES6 object then it will. If you don't then it won't. It has no idea what properties some other module is expecting or how that other thing will handle their presence or absence. You have to read the MQTT docs to figure that out. The only thing ES6's shorthand is doing for you here is that if you omit the value of a property (the colon and whatever follows), it uses the value of the variable with the same name as the property. Commented May 13, 2016 at 3:43
  • 1
    It sounds like you're missing the basic idea of the new ES6 syntax...the only new feature relevant here is that if you have a property named foo and a variable named foo, you can just write foo instead of foo: foo. Otherwise it's like ES5 object literal syntax. Commented May 13, 2016 at 3:45

2 Answers 2

1

I'm not sure this answers your question, but Object.assign might help:

var options = getOptionsFromMQTT();

Object.assign(options, {options, {clientID, keepAlive});
// options now uses whatever values are assigned to clientID and keepAlive

More from MDN:

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

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

Comments

1

If you pass mqtt.Client an object with a property value of undefined, it will use its default.

function createClient(streamBuilder, clientId, keepAlive, reconnectPeriod, clean, will) {
    let options = {clientId,keepAlive,reconnectPeriod, clean, will};
    mqtt.Client(streamBuilder, options);
}

For example will use mqtt's default values for every option but clientId, and keepAlive when called as follows: createClient(builder, 'a1', 0); If you do not intend to ever use a property, omit it and do not pass it as an option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.