Very simple question but I'm quite new to JS and this is not easy to search.
If I see a line like this:
var colours = {}, r, g, b;
I get that it's just a declaration for 3 vars, but what does the {} bit mean? I can't see anywhere that gives me an idea as to what this is declaring?
Thanks!
Very simple question but I'm quite new to JS and this is not easy to search.
If I see a line like this:
var colours = {}, r, g, b;
I get that it's just a declaration for 3 vars, but what does the {} bit mean? I can't see anywhere that gives me an idea as to what this is declaring?
Thanks!
Share Improve this question edited May 11, 2012 at 12:55 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked May 11, 2012 at 12:51 FBryant87FBryant87 4,5955 gold badges50 silver badges87 bronze badges 1- 1 possible duplicate of What do curly braces in javascript mean? – Felix Kling Commented May 11, 2012 at 12:54
7 Answers
Reset to default 7It declares new object and equivalent to new Object();
var colours = {}, r, g, b;
This declares 4 variables which is the same as
var colors = {}; // this is an empty object like, var colors = new Object();
var r; // undefined
var g; // undefined
var b; // undefined
It means that colours is going to be an object.
{}
declares an object, with no members. Like an empty data container.
[]
would declare an empty array.
Arrays have number indexes (and a few handy methods) and objects can have string indexes (but lack the methods of an array)
It's a declaration of four variables, not three. One is called colours
and is initialized to {}
. The others are called r
, g
, and b
, and their initial values are undefined
. {}
is an empty object literal.
It's declaring an empty object literal.
It initializes colors
with a new, empty object.
While objects in JavaScript can have methods, too, they are often used as associative arrays, which is pretty likely in this case (assumption based on the name and the fact that it's initialized without any properties).