I have the following Dygraph object being created:
function prime() {
g = new Dygraph(
document.getElementById("g"),
data, //This is the variable I want to avoid passing in for older browsers
{
labelsDivStyles: { 'textAlign': 'right' },
title: document.getElementById("title").value,
titleHeight: 35,
labelsSeparateLines: true,
includeZero: true
}
);
}
And I want to have a browser check that determines whether to include the "data" such as:
if(Browser.Version() > 8){
//include data variable
} else{
//don't include
}
Does anyone have any ideas outside of creating a second function to deal with the other case
You either need to create and manipulate the object literal beforehand, or you need to alternate with a ternary operator between value and null / undefined .
var myObj = {
labelsDivStyles: { 'textAlign': 'right' },
title: document.getElementById("title").value,
titleHeight: 35,
labelsSeparateLines: true,
includeZero: true
};
if( Browser.Version() > 8 ) {
myObj.data = 42;
}
or
function prime() {
g = new Dygraph(
document.getElementById("g"),
data,
{
labelsDivStyles: { 'textAlign': 'right' },
title: document.getElementById("title").value,
titleHeight: 35,
labelsSeparateLines: true,
includeZero: true,
data: Browser.Version() > 8 ? 42 : null
}
);
}
You might also choose to have a self-invoking function which does the job and returns that object, but thats pretty much the same as example #1. Either way, you can't tell ECMAscript to create or to not-create a key while forming an object literal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.