Reputation: 5726
I'm not even quite sure what the correct vocabulary is for this, but I have the following code:
$(this).mySpecialMethod({
propertyOne: "ValueOne",
propertyTwo: "ValueTwo"
});
I'd like to dynamically control passing in a special propertyThree: "ValueThree"
to this depending on other properties on the page/control.
How do I declare an object, add new properties to it based on my requirements and then pass it to the above specified method?
Upvotes: 1
Views: 3405
Reputation: 69346
You can create an object like this:
var myObj = {
propertyOne: "ValueOne",
propertyTwo: "ValueTwo"
}
and then add to it as many properties as you want using the right syntax, like this:
if (/* some condition */) {
myObj.propertyThree = "ValueThree";
}
Now you can pass the object you created to your method:
$(this).mySpecialMethod(myObj);
I suggest you to take a look at the MDN Documentation about Objects in JavaScript: you may find it helpful.
Upvotes: 4
Reputation: 21759
Try as follows:
var options = {
propertyOne: "ValueOne",
propertyTwo: "ValueTwo"
};
if (conditionIWant === true) {
options.propertyThree = 'ValueThree';
}
$(this).mySpecialMethod(options);
Upvotes: 1