0

I'm trying to get this to work, but it doesn't:

var i; 

i.test = function() { 
    alert("hello"); 
}

i.test();

I expect the code to alert 'hello', but instead, the Firefox error console shows:

missing } in XML expression
alert("hello"); 
---------------^

How do I fix this...

3 Answers 3

4

Your i isn't assigned to anything so it's not an object. It is, in fact, pointing to the global undefined object which happens to be read-only in Firefox (as it should be). You need:

var i = {}; //init to empty object

then all will be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I did as you suggested, and all is well. Thanks!
0

You can't add a function to an undefined value, you need to create an actual object:

var i = {};

Although not required, you should have a semicolon at the end of the statement to avoid ambiguity:

i.test = function() { 
  alert("hello"); 
};

Comments

0
var i = {};
i.test = function() { 
    alert("hello"); 
};

You had two separate issues. You were not initializing i (as noted by slebetman), and you were missing a semi-colon, forcing the interpreter to use semi-colon replacement.

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.