简体   繁体   English

jQuery中没有分号的语句

[英]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. 该语句是我正在使用的jQuery树插件的一部分。现在,它不允许我在该语句的末尾添加半冒号,并且需要将其放在If条件中以基于某个值调用操作。 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 . 属性初始值设定项的右侧是一个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. 在这种情况下,“ url”是对象内部的键。 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语句中使用url的值,则

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的一部分。 JSON syntax looks like this: JSON语法如下所示:

{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. 但是,JSON实际上只是一个对象。

This means that whatever JSON you have is really just an Object. 这意味着无论您拥有什么JSON,实际上都只是一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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