Could you explain an object literal as simply as possible please?
javascript - How to create a method in object literal notation? - Stack Overflow
What do people mean when they say "object literal" in JavaScript? - Stack Overflow
javascript - What is the meaning of "literal" in the phrase object literal notation? - Stack Overflow
Videos
Syntactically, the change is very simple :
var bob = {
age: 30,
setAge: function (newAge) {
bob.age = newAge;
}
};
But as you can see, there's a problem : as in your code it uses the external bob variable so this wouldn't work if you change the value of the bob variable.
You can fix that with
var bob = {
age: 30,
setAge: function (newAge) {
this.age = newAge;
}
};
Note that at this point you should check whether what you need isn't, in fact, a class, which would bring some performance improvements if you have several instances.
Update: ECMAScript 6 now allows methods to be defined the same way regardless of whether they are in an object literal:
var bob = {
age: 30,
setAge (newAge) {
this.age = newAge;
}
};
Its nothing different and as easy as
var bob = {
age: 30,
setAge: function( newAge ) {
this.age = newAge;
}
};
Alternatively, you can create a real setter function either by invoking Object.defineProperty() or as simple as
var bob = {
age: 30,
firstName: 'j',
lastName: 'Andy',
set setName( newName ) {
var spl = newName.split( /\s+/ );
this.firstName = spl[ 0 ];
this.lastName = spl[ 1 ];
}
}
Where you could go like
bob.setName = "Thomas Cook"; // which sets firstName to "Thomas" and lastName to "Cook"
Yes, that's exactly what object literal means. A definition of a javascript object which may contain comma separated definition of properties, functions, ...
Object literals are formed using the following syntax rules:
- A colon separates property name from value.
- A comma separates each name/value pair from the next.
- There should be no comma after the last name/value pair. Firefox won't object if you add it, but Internet Explorer will trigger an error: 'Expected identifier, string or number'.
An object literal is a way to declare an object.
You would write
var myObject = {}; // with or without members
instead of
var myOject = new Object();
You can also use array literals:
var myArray = [];
instead of
var myArray = new Array(); // with or without members
It's shorter, of course but it also bypasses the constructor so it's supposed to be more efficient.
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).
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".
Computed property names are supported in ECMAScript2015:
var name = 'key';
var value = 'value';
var o = {
[name]: value
};
console.log("o as json : " + JSON.stringify(o));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer
Prior to ECMAScript 2015 (ed 6), an object literal (ECMAScript calls it an "object initializer") key must be one of:
- IdentifierName
- StringLiteral
- NumericLiteral
So you couldn't use an expression as the key in an initialiser. This was changed as of ECMAScript 2015 (see below). You could use an expression with square bracket notation to access a property, so to set the properties with an expression you had to do:
var required = { directories : {}};
required.directories[this.applicationPath] = "Application " + this.application + " does not exists";
required.directories[this.applicationPath + "/configs"] = "Application config folder does not exists";
...
and so on. Since this.applicationPath is reused a lot, better to store a reference to help with performance and cut down the amount of code:
var a = this.applicationPath;
var required = { directories : {}};
var rd = required.directories;
rd[a] = "Application " + this.application + " does not exists";
rd[a + "/configs"] = "Application config folder does not exists";
...
Edit
As of ECMAScript 2015 (ed 6), object initializers can have computed keys using:
[expression]: value
There is also shorthand syntax for property and method names.
See MDN: Object Initializer or ECMAScript Object Initializer.