Reputation: 23941
In Javascript: The Good Parts Douglas Crockford writes that one of Javascript's good ideas is "expressive object literal notation." I understand that he is basically complimenting 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?
Upvotes: 47
Views: 23870
Reputation: 1
Literal type is the value itself, i. e., if we define:
let x: 'apartment' | 'house';
x can only be those literal types. Literal means strictly only these values
A non-literal type is the common types, like string, number, boolean... those can be any values inside their subsets of value. I. e, if we define:
let x: string;
Now x can be any string, its not literally a string. Non-literal means an infinite set of values
So, if you hear about non-literal types, we are referring to the generic types. And if you hear about a literal type, we are referring to a very specific and strict value that can the assigned to a type.
Edit: Adding another example of where your can use the concept of literal types =>
In typescript, there is a concept called "Excess property checking", which is the one where ts will check if every property in the type you create is present on the value you want to assign to that type. Take a look at the following example:
interface WebResponse {
title: string;
hasError?: boolean;
}
let response: WebResponse = {
title: 'New response',
haserror: true
}
See that one property is misspelled, "haserror" instead of "hasError". So ts will throw this in compile time to help you. Lets read its error:
Type '{ title: string; haserror: boolean; }' is not assignable to type 'WebResponse'.
**Object literal** may only specify known properties, but 'haserror' does not exist in type 'WebResponse'. Did you mean to write 'hasError'?(2322)
By saying "Object literal", typescript is referring to this part of the code:
= {
title: 'New response',
haserror: true
}
This is an object literal! Its the value of the object. We can get rid of this error by saying:
let otherResponse = {
title: 'New response',
haserror: true
}
let newResponse : WebResponse = otherResponse
Wow! What's the difference? Now we are assigning to a non-literal, which is "otherResponse". This is an non-literal type for the same reason the variable "x" above is a non-literal too, because it can be any value.
So ts says "if this can be any value, I am not going to Excess property check on him!"
And we can conclude that
Excess property checking only applies to object literals
Thats an example where you can apply the concept of Object Literal
Check the result yourself on typescript playground (https://www.typescriptlang.org/play):
interface WebResponse {
title: string;
hasError?: boolean;
}
let response: WebResponse = {
title: 'New response',
haserror: true
}
let otherResponse = {
title: 'New response',
haserror: true
}
let newResponse : WebResponse = otherResponse
Upvotes: 0
Reputation: 1626
I've been struggling with this myself. While I found the answers above helpful, I found a simple definition for literal
in Webopedia:
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.
What really stuck out to me was
But a literal is not a name -- it is the value itself.
Additionally, this StackOverflow post is also very helpful. I appreciated this comment:
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.
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.
Upvotes: 5
Reputation: 10822
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 word
is a string literal; you have created something that is literally of the string type.
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.
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.
A non literal way is to use an object constructor - a function that is combined with the new
keyword to make a new object.
let myConstructedObject = new Object();
Here the new key word is treated as an operator. And as such, the data inside myConstructedObject
is the result of an expression evaluation.
Upvotes: 10
Reputation: 2013
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.
So in an OOP language an object literal would be something like:
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".
Upvotes: 10
Reputation: 12795
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:
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:
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
Upvotes: 6
Reputation: 82814
About "complimenting JSON": He specified it.
The "literal" part: Googling "object literal" provides two top resources: MDN and Wikipedia. To quote the latter:
In computer science, a literal is 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).
Upvotes: 36