1

I've seen the folllowing:

var field = {};
field.tf = $("#textfield");
field.date = $("td#CurrentDate");
field.name = $("input#myname");

what does the initialization var field = {} do? Is it creating a generic object (field) on which properties (field.tf) can be set on the fly?

Fabii
  • 3,820
  • 14
  • 51
  • 92

8 Answers8

10

JavaScript has a number of different literal syntaxes beyond the standard Number, String, Boolean that are common to other languages:

foo = {};

is the same as:

foo = new Object();

While

foo = [];

is the same as:

foo = new Array();

and

foo = /foo/;

is the same as:

foo = new RegExp('foo');

These literal syntaxes have their own quirks and nuances. For objects, key-value pairs are comma separated with the keys and values separated by colon characters:

foo = {
    bar: 'baz',
    fizz: 'buzz'
};

is equivalent to:

foo = new Object();
foo.bar = 'baz';
foo.fizz = 'buzz';

For arrays, the array members are simply comma separated:

foo = ['bar', 'baz'];

is equivalent to:

foo = new Array();
foo.push('bar', 'baz');

Note that for arrays the constructor function has a flaw. new Array(1, 2, 3) is equivalent to [1, 2, 3], but new Array(3) is not equivalent to [3], it is instead equivalent to [undefined, undefined, undefined] (an array of size 3 with no members).

This convenient initialization structure, and the ability to nest objects and arrays within each other is what lead to the formalization of the JSON data interchange format

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
2
var field = {};

It represent an object datatype.

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • 4
    Not sure what `Kindly of saying that it is going to hold objects.` means, but everything is an object in JavaScript – Chad Sep 05 '13 at 15:01
  • @Chad yes right, What I tried to convey is "A kind of predefined state" which is equivalent to `Object [] obj = new Object[];` in C#. Please correct me if I was wrong. – Praveen Sep 05 '13 at 15:21
  • saying `field={}` doesn't imply it will hold any data type, it only tells you that `field` is an instance of a plain object. It can hold anything: `field={ regx: /hey/, str: 'hey', obj: {}, fn: function() {} };` – Chad Sep 05 '13 at 16:35
2

A generic object.

It is the same as:

var field = {
   tf: $("#textfield"),
   date: $("td#CurrentDate"),
   name: $("input#myname")
};
letiagoalves
  • 11,224
  • 4
  • 40
  • 66
2

Is it creating a generic object (field) on which properties (field.tf) can be set on the fly?

Answer: Yes, that's exactly what it's doing.

For what it's worth, the code in the question could be simplified by defining all the properties in the initial declaration:

var field = {
    'tf'   : $("#textfield"),
    'date' : $("td#CurrentDate"),
    'name' : $("input#myname")
};

That does exactly the same as the original code, but a bit neater.

Spudley
  • 166,037
  • 39
  • 233
  • 307
2

var something = {} defines an object in JavaScript. So if you do not know what {} means, I assume that you don't have a full understanding of the 3 other lines of code as well. Let's give it a try:

So "field" is an object and you can give any structure to this object. It can have height, width, dimension and whatever you like. So, we defined our object like this:

var field = {};

Now, how do you add the properties to the object? In object oriented programming, we have the dot(.) notation. For example: person.name, requests the name of a person. According to your code:

field.tf = $("#textfield");
field.date = $("td#CurrentDate");
field.name = $("input#myname");

a) First you initialize the tf of the field to the value of your text box.
b) Secondly you initialize the date of the field to the value of your text box.

c) And finally you initialize the name of the field to the value of your text box.

You could also define your object as:

var field = {
'tf'   : $("#textfield"),
'date' : $("td#CurrentDate"),
'name' : $("input#myname")
};

I hope that helps! Cheers

Stelios Voskos
  • 526
  • 5
  • 17
1

That's just the standard notation for creating an empty JavaScript object. You'll see short-hand like this quite frequently:

var object = { };
var array = [ ];

Think of it as the minimalist version of a declaration:

var object = {
  key1: 'value1',
  key2: 'value2'
}

var array = [ 1, 2, 3, 4 ];
tadman
  • 208,517
  • 23
  • 234
  • 262
1

It is the same as saying:

var field = new Object();

Another example is creating an array: var myArr = [ ] vs var myArr = new Array();

ComFreek
  • 29,044
  • 18
  • 104
  • 156
jOshT
  • 1,525
  • 1
  • 11
  • 15
1

It is creating a javascript object by directly assigning object literal to it.

Object literals provide a very convenient notation for creating new object values. An object literal is a pair of curly braces surrounding zero or more name/value pairs. An object literal can appear anywhere an expression can appear:

var empty_object = {};
var stooge = {
   "first-name": "Jerome",
   "last-name": "Howard"
};
Moazzam Khan
  • 3,130
  • 2
  • 20
  • 35