简体   繁体   English

需要澄清何时在JavaScript中使用引号,何时不使用引号

[英]Need clarification on when to use quotes and when not to in JavaScript

I'm new to JavaScript following the courses from codecademy.com. 我是来自codecademy.com的课程的JavaScript新手。 Recently they introduced bracket notation (as opposed to the simpler dot notation). 最近,他们引入了括号符号(与更简单的点符号相反)。 I understand there are advantages because you can reference things that you can't reference using dot notation. 我知道有很多优点,因为您可以使用点表示法来引用无法引用的内容。 However I don't understand why I'm supposed to use quotes in certain circumstances. 但是我不明白为什么在某些情况下应该使用引号。

For example: 例如:

var suitcase = {
    shorts: "purple"
};
if (suitcase.hasOwnProperty("shorts")) {
    console.log(suitcase.shorts);
}

Why does shorts need quotes around it in the line if (suitcase.hasOwnProperty("shorts")) { ? if (suitcase.hasOwnProperty("shorts")) {为什么短裤需要用引号引起来呢? I'm used to quotes designating something as a string. 我习惯用引号将某些东西指定为字符串。 shorts is a property of the suitcase object so I would think it would be referenced without quotes. 短裤是手提箱对象的属性,因此我认为不带引号就可以引用短裤。 So far I've been doing well understanding the rules of JavaScript but this wasn't clearly explained so I find myself confused. 到目前为止,我一直很好地理解JavaScript的规则,但是并没有清楚地解释这一点,因此我感到困惑。

All objects' properties' names are just strings. 所有对象的属性名称都只是字符串。 You can always refer to a property by a string name like a["b"] . 您始终可以通过诸如a["b"]的字符串名称来引用属性。 The one exception is when you use a . 一个例外是使用时. . In that case, ab is a property. 在这种情况下, ab是一个属性。 This is equivalent to a["b"] . 这等效于a["b"] In all other cases, use strings to refer to property names. 在所有其他情况下,请使用字符串来引用属性名称。

hasOwnProperty is a normal method. hasOwnProperty是正常方法。 If you had passed in shorts rather than "shorts" , JS would have treated shorts as a variable. 如果您传递的是shorts而不是"shorts" ,那么JS会将shorts视为一个变量。

Consider this: 考虑一下:

var suitcase = {
    shorts: 'purple',
    shorts: purple
}

yes, it's assigning to 'shorts' twice. 是的,它两次分配给“短裤”。 But the first line, 'purple' is assigning a STRING whose value is purple to the shorts object key. 但是第一行'purple'为短裤对象键分配了一个值为purple的STRING。 The second line, is assigning a VARIABLE named purple , which has not yet been defined. 第二行,分配一个尚未定义的名为purple的变量。

Context is everything in Javascript. 上下文是Javascript中的一切。 There's no need to quote the shorts portion, because the JS interpreter knows you'r defining an object key there. 不需要引用shorts部分,因为JS解释器知道您正在那里定义对象键。 But it's NOT smart enough to decide if purple should be a string representing a color, or a variable whose name happens to be purple . 但是,判断purple是否应该是代表颜色的字符串或名称恰好是purple的变量,还不够聪明。 That's why there's quotes - to provide the necessary context. 这就是为什么要使用引号-提供必要的上下文。

Ditto for the hasOwnProperty . hasOwnProperty If shorts isn't quoted, then it's treated as an undefined variable. 如果未引用短裤,则将其视为未定义的变量。 if it IS quoted, then it's a string. 如果带引号,则为字符串。

Your example is the same as: 您的示例与以下示例相同:

    suitcase = new Object();  
    suitcase.shorts = 'purple';
    suitcase.hasOwnProperty('shorts');   //returns true  

Always remember that arguments/parameter in javascript not enclosed in qoutes is variable. 始终记住,javascript中未包含在qoutes中的参数/参数是变量。 Not just javascript actually, all languages. 实际上不仅是JavaScript,还包括所有语言。 Do not confuse yourself with variables. 不要将自己与变量混淆。

Javascript expects a variable shorts . Javascript期望变量shorts

    var shorts = 'shorts';
    suitcase.hasOwnProperty(shorts);   //returns true

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty https://developer.mozilla.org/zh-CN/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

the hasOwnProperty() function needs the name of the property you want to check for. hasOwnProperty()函数需要您要检查的属性的名称。 Therefore it needs to be passed in as a string. 因此,它需要作为字符串传递。 If you leave off the quotes then javascript will assume you're passing a variable containing the property name. 如果省略引号,则javascript将假定您正在传递包含属性名称的变量。

Without the quotes javascript is going to interpret shorts as an object and since shorts is undefined you get. 如果没有引号,则javascript将把短裤解释为一个对象,因为短裤是未定义的,您会得到。 The hasOwnProperty function is looking for a property name of type string. hasOwnProperty函数正在寻找字符串类型的属性名称。

ReferenceError: shorts is not defined

so 所以

suitcase.shorts

returns "purple" 返回“紫色”

shorts !== suitcase.shorts

shorts is not the same object as suitcase.shorts, in fact it is not even an object until you declare it. 短裤和手提箱。短裤不是一个对象,实际上,直到声明它,它甚至都不是对象。

shorts = "brown"

This sets the value of shorts to "brown", but suitcase.shorts is still "purple" 这会将短裤的值设置为“棕色”,但是手提箱。短裤仍然是“紫色”

I hope this clears that up. 我希望这一点可以解决。

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

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