0

Despite excessive googling I just don't get why my function doSomething does nothing in the situation below. Any idea why it doesn't work?

Many thanks, Gordon

var arrAttend=new object();
arrAttend["Blob"]='hello';

function doSomething() {
alert (arrAttend["Blob"]);
}
2
  • What is your output? How are you invoking doSomething()? Commented Feb 10, 2013 at 10:41
  • dosomething is invoked later in code by a mouseclick on a td object. I understand it is better to use an object as an associative array, hence the use of the object to store data - this is simply a test to try and make it work. Literal syntax suggestion below has worked. Commented Feb 10, 2013 at 22:33

5 Answers 5

4

It's a typo, you should use new Object (capital O). Or use an Object Literal:

var arrAttend = {Blob: 'hello'};

function doSomething() {
  alert (arrAttend.Blob);
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for literal, literals are always better (except when they're not, but here they are).
Thanks for this - capitalising made no difference, but the literal syntax did work! Am happy as long as I have A method :)
1

Two problems :

  • object isn't defined
  • you don't call your function

Try this :

var arrAttend= {}; // that's the simplest way to create a new javascript object
arrAttend["Blob"]='hello';

function doSomething() {
   alert (arrAttend["Blob"]);
}
doSomething();

Note that the first kind of error is very easily found when you look at the console : an error is displayed. I'd suggest you to use developer tools (for example Chrome's ones) so that you don't develop in the blind. BTW you'd see that using console.log instead of alert is most often more convenient.

1 Comment

the function call was elsewhere in a mouseclick. I tried your syntax above but if checked in the function arrAttend is undefined... Literal syntax above seems to have worked. Thanks!
0

Try this :

var arrAttend=new Object();
arrAttend["Blob"]='hello';

function doSomething() {
alert (arrAttend["Blob"]);
}

Comments

0

There's typo error in your code. And an object should be used like follow -

var arrAttend= {
        name:'Blob'
    };
function doSomething() {
alert (arrAttend.name);
}
      doSomething(); 

1 Comment

You're defining arrAttend twice, first setting it to an empty object and then resetting it to a new object (constructed from the object literal).
0

Try this:

// create object
var arrAttend=new Object();
arrAttend["Blob"]='hello';

function doSomething() {
alert (arrAttend["Blob"]);
}

// call function
doSomething();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.