javascript 短语对象文字符号中的“文字”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20998159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:47:47  来源:igfitidea点击:

What is the meaning of "literal" in the phrase object literal notation?

javascriptjson

提问by bernie2436

In Javascript: The Good PartsDouglas Crockford writes that one of Javascript's good ideas is "expressive object literal notation." I understand that he is basically complimenting JSON.

Javascript: The Good Parts 中,Douglas Crockford 写道,Javascript 的一个好主意是“富有表现力的对象文字符号”。我知道他基本上是在赞美JSON

But what is "literal" about this notation? Are there are other languages that use "expressive object literal notation?" Are there languages that don't? What exactly does he mean?

但是这个符号的“字面意思”是什么?是否还有其他语言使用“表达性对象文字表示法”?有没有没有的语言?他究竟是什么意思?

采纳答案by Boldewyn

About "complimenting JSON": He specified it.

关于“赞美 JSON”:他指定了它。

The "literal" part: Googling "object literal" provides two top resources: MDNand Wikipedia. To quote the latter:

“文字”部分:谷歌搜索“对象文字”提供了两个顶级资源:MDNWikipedia。引用后者:

In computer science, a literalis a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, and usually for booleans and characters; some also have notations for elements of enumerated types and compound values such as arrays, records, and objects.

在计算机科学中,文字是表示源代码中固定值的符号。几乎所有的编程语言都有原子值的符号,例如整数、浮点数和字符串,通常用于布尔值和字符;有些还具有枚举类型元素和复合值(例如数组、记录和对象)的符号。

Basically, all syntax constructs whose use lead to a defined type can be called a literal. (E.g., a string literal, "abc".) It's a technical term that denotes, that "literally" writing something in this or that way leads to a certainly typed variable exclusively(in contrast to constructs, that look like something else, like array()in PHP).

基本上,所有使用导致定义类型的语法结构都可以称为文字。(例如,一个字符串,"abc")这是一个技术术语,表示,在这一点,“字面意思为”写的东西还是那条路通向一定类型的变量专门(与结构,看起来像别的东西,像array()在PHP) .

回答by Aage Torleif

Well, in programming in general a literal is a fixed value. Like saying var five = 5;and using "five" in some math, just use the number 5 literally.

嗯,一般在编程中,文字是一个固定值。就像var five = 5;在某些数学中说和使用“五”一样,直接使用数字 5。

So in an OOP language an object literal would be something like:

因此,在 OOP 语言中,对象字面量类似于:

var new_dog = {
    name: "doggy",
    good_dog: false
};

The entire thing is my object. Things between my {} are my literals. My notation is a pattern "name:value".

整个事情就是我的对象。{} 之间的内容是我的文字。我的符号是一个模式“名称:值”。

回答by cham

As someone who is new to programming I've really struggled with the idea of literals. Explanations often assume that you already know all about types and constructors. So this is my (over)simplified take on it:

作为一个刚接触编程的人,我真的很纠结于文字的概念。解释通常假设您已经了解所有关于类型和构造函数的知识。所以这是我(过度)简化的看法:

By 'type', we mean the classification of the variable (different types of variables can do different things). ?It's common to talk about primitive-type literals? such as strings, numbers, and booleans. Here is a string literal example:

“类型”是指变量的分类(不同类型的变量可以做不同的事情)。? 谈论原始类型文字很常见吗?例如字符串、数字和布尔值。这是一个字符串文字示例:

let myStr = 'word'; 

In this case wordis a string literal; you have created something that is literally of the string type.

在这种情况下word是一个字符串文字;您已经创建了字面意义上的字符串类型的东西。

In JS you can have object literals as well. That is you can just type out this thing between curly braces and it is automatically a thing of object-type.

在 JS 中,您也可以拥有对象字面量。也就是说,你可以在花括号之间输入这个东西,它自动成为对象类型的东西。

let mylitteralObject = {};

We have created an object without using an operator (an operator is essentially a built in function using familiar syntax, like '+'). The JS engine looks at the curly braces and works out that you are declaring an object.

我们已经创建了一个不使用运算符的对象(运算符本质上是一个使用熟悉的语法的内置函数,如“+”)。JS 引擎查看花括号并确定您正在声明一个对象。

A non literal way is to use an object constructor - a function that is combined with the newkeyword to make a new object. ?

一种非文字方式是使用对象构造函数 - 一个与new关键字组合以创建新对象的函数。?

let myConstructedObject =  new Object();

Here the new key word is treated as an operator. And as such, the data inside myConstructedObjectis the result of an expression evaluation.

此处将新关键字视为操作符。因此,里面的数据myConstructedObject是表达式评估的结果。

回答by Alexander

An object literal is a comma separated list of name value pairs wrapped in curly braces. In JavaScript an object literal is defined as follows:

对象字面量是用大括号括起来的逗号分隔的名称值对列表。在 JavaScript 中,对象字面量定义如下:

var someObject = {
    some_prop: 'some string value',
    another_prop: 'another string value',
    int_prop: 100
};

It is “flat”. You create it, you add properties and methods to it, and all of those properties and methods are public . Object literals are formed using the following syntax rules:

它是“平的”。您创建它,向其添加属性和方法,并且所有这些属性和方法都是 public 。对象字面量是使用以下语法规则形成的:

  • A colon separates property name from value.
  • A comma separates each name/value pair from the next.
  • There could be no comma after the last name/value pair, But in this case Internet Explorer prior to version 9 will generally trigger an error: 'Expected identifier, string or number'.
  • 冒号将属性名称与值分开。
  • 逗号将每个名称/值对与下一个分开。
  • 在最后一个名称/值对之后不能有逗号,但在这种情况下,版本 9 之前的 Internet Explorer 通常会触发错误:“预期标识符、字符串或数字”。

Values can be of any data type, including array literals, functions, and nested object literals .

值可以是任何数据类型,包括数组文字、函数和嵌套对象文字。

Although a JavaScript object literal and a JavaScript instance object are both objects, they differ in their inherent nature and features

尽管 JavaScript 对象字面量和 JavaScript 实例对象都是对象,但它们的内在性质和特征有所不同

回答by Dani Amsalem

I've been struggling with this myself. While I found the answers above helpful, I found a simple definition for literalin Webopedia:

我自己一直在努力解决这个问题。虽然我发现上面的答案很有帮助,但我literalWebopedia 中找到了一个简单的定义:

In programming, a value written exactly as it's meant to be interpreted. In contrast, a variable is a name that can represent different values during the execution of the program. And a constant is a name that represents the same value throughout a program. But a literal is not a name -- it is the value itself.

A literal can be a number, a character, or a string. For example, in the expression,

x = 3

x is a variable, and 3 is a literal.

在编程中,完全按照要解释的方式编写的值。相反,变量是在程序执行过程中可以代表不同值的名称。常量是在整个程序中代表相同值的名称。但是文字不是名称——它是值本身。

文字可以是数字、字符或字符串。例如,在表达式中,

x = 3

x 是变量,3 是文字。

What really stuck out to me was

真正让我印象深刻的是

But a literal is not a name -- it is the value itself.

但是文字不是名称——它是值本身。

Additionally, this StackOverflow postis also very helpful. I appreciated this comment:

此外,这篇StackOverflow 帖子也非常有帮助。我很欣赏这条评论:

Basically code that represents a value in a program instead of having special meaning for the program to execute. For example: 15 literally means fifteen in the program vs + which has the special meaning of "add two things together". You wouldn't treat + as representing the plus sign itself since it is not a literal.

基本上是表示程序中的值的代码,而不是对要执行的程序具有特殊意义。例如:15在程序中的字面意思是15个vs+,有“两件东西相加”的特殊含义。您不会将 + 视为表示加号本身,因为它不是文字。

And this answer was helpful:

这个答案很有帮助:

Quick example:

int my_int_var = 723;

723 - This set of characters refers to a literal integer value.

my_int_var - This set of characters refers to a variable integer value.

快速示例:

int my_int_var = 723;

723 - 这组字符指的是文字整数值。

my_int_var - 这组字符指的是一个可变整数值。