简体   繁体   中英

statement in jQuery without semicolon

I have a statement like below ,

url : "expand.do?parentID=" + node.id,

This statement is a part of jQuery tree plugin that I am using.Now, it is not allowing me to put semi colon in the end of this statement and I need to put it in If condition to call action based on some value. If I use statement like this only then it works fine.

That isn't a statement, it's a property initializer , presumably followed by other property initializers. It sounds like you're using them in an argument being passed to a function, eg:

someFunctionCall({
    prop1: "value 1",
    prop2: "value 2"
});

Property initializers are separated from subsequent property initializers with commas, not semicolons, which is why adding a semicolon won't work.

The right-hand side of a property initializer is an expression . If you need to do conditional logic in that expression, you can use the conditional operator ( ? : ), eg:

someFunctionCall({
    prop1: someCondition ? "value 1" : "value 2",
    prop2: "..."
});

If you need to do something more complex than you can readily do in an expression, or if you need to access those properties in subsequent logic, you can split things up:

var params = {
    prop2: "..."
};
if (someCondition) {
    params.prop1 = "value 1";
} else {
    params.prop1 = "value 2";
}
// and/or
if (params.prop2 === "foo") {
    // ....
}
someFunctionCall(params);

In this case "url" is a key inside an object. Based on the comma you have there is looks there are multiple keys in this object.

var foo = { url : "expand.do?parentID=" + node.id, bar: "blah"}

If you want to use the value of url in an IF statement then

if(foo.url){ 
   //do stuff
}

Try this;

{url : getURL(node),

and

function getURL(node){
    if (node.something == 'something'){
     var node_id = node.id
    }else
    {
     var node_id = (node.id + 22)
    }
    return "expand.do?parentID=" + node_id
}

This is most likely some part of JSON. JSON syntax looks like this:

{url : "expand.do?parentID=" + node.id}

Usually, there are more elements:

{
    hello: 'world',
    'I can even have spaces and Funny characters: 1 * 1 = 2 and PI: π': '3.14159265359'
}

However, JSON is really just an object.

This means that whatever JSON you have is really just an Object.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM