Please help me also show me how to apply them in a simple example.

  • I also recommend you to have "JavaScript: The Definitive Guide" Link : Aaron almost 10 years ago

3 answers

Aaron 45

I want to talk about a little bit more advanced thing about Arrays and Objects. Objects As Associative Array : It is also possible to use the [ ] operator,which is more commonly used with Arrays, to access these properties. Thus, the following two JS expressions have the same value :;

The important thing here is the at the first example the property name is a identifier and at the second one, property name is a string, but why is there an implementation like that :

But how can I use that ? :

var addr = " ";
     addr += customer["address"+i] + "\n"; }

customer is an object btw

You can't simply say if you have an object which is :

var customer = {address1:"Irvine",address2:"Los Angeles",address3:"Costa Mesa"}; 

So associative arrays would be nice choice :

var addr = " ";
         addr += customer.address +i + "\n"; }

So as you can see you can dynamically iterate an object by its property using object as associative arrays method.

just fyi.

PS : Arrays are special objects which have special methods to use arrays easily. That's why you can use Array notation in Object types.

Answered almost 10 years ago by Aaron
danwellman 5600

JavaScript objects are data structures similar, but more powerful than an array. An array is a collection of data items where each item has an index number. An object is similar but instead of having an index number, it has a name (or key) that is used to refer to the items in the object An object literal is defined like this:

var myObject = {};

The variable myObject would now contain an empty object. Or an object can be defined using the new keyord, like this:

var myObject is new Object();

Personally, I almost always use the object literal syntax because it is more compact. So the object is the logical container for a series of labelled data items.

Properties are what you find within objects. So when defining an object literal you can add properties like this:

var myObject = {
  key1: value1,
  key: value2

In this example there are 2 properties. Each property has 2 components, a label or key, followed by a value. The two items in the property are separated by a colon, and the two properties are separated by a comma.

If you have an existing object (created either with the new keyword or literally) you can assign properties using dot (.) notation:

myObject.key1 = value1;

You can also assign variables as keys using bracket([ ]) notation:

myObject[variableName] = value1;

This time the key will be whatever is contained in the variableName variable, it won't just say 'variableName'.

Methods are simply properties that have a function as their value, like this:

var myObject = {
  key1: function() { alert("Objects are amazing!") },

So to recap - objects are the outer containers for data, properties are the individual items of data (the key:value pairs) and methods are just functions that appear as the value of a property :)

Answered almost 10 years ago by danwellman

Well, in JavaScript, the whole concept get's kinda wacky.

An object is a data type vs properties and methods which are a part of the object data type. An object can contain both properties (with values) and methods.


var demoObject = {
    property: 'value', // this is a property, it "stores" something (string, number, boolean)
    method: function(newValue) {
        // this is a method that "does" something = newValue;
} = 'my value';
alert(; // will alert "my value"

demoObject.method('my other value');
alert(; // this will now alert "my other value"
alert(demoObject['property']); // just to make things confusing, you can also do this; this is useful when your property is a number or something that isn't a valid variable name, such as "1a"

So, as you can see, a property can store a value while a method does something, possibly with the properties of the object. The object is the container for both and therefore passing it into a function or another object will allow that function or object to access the values of the properties and use the methods.

The other confusing issue is that functions act similar to values, as method (in the above example) is a property of the object that has a value of a function (which is also an object).

And another confusing bit: in JavaScript, arrays are actually objects. So...

var array = [ 1, 2, 3 ];
alert(array[1]); // will alert 2 because it's the second value in the array
alert(array.length); // will alert 3 because the array has 3 values and this notation can be used because the array is an object and therefore has a property of length

Most of this you don't need to know in your every day JavaScript coding, but it's good to know you can do things like:

function Blah(param1, param2) {
    if (arguments.length == 1) param2 = 2;
    return param1 * param2;
alert(Blah(2)); // will alert 4 because param2 is set to 2 when it isn't passed

The above can be done because the function is an object.

Answered almost 10 years ago by Darryl Hein