views:

118

answers:

6

I have an object

var object= {}

I put some data in the object and then I want to print it like this

document.write(object.term);

the term is a variable that changes depending on different situations. When I try printing this it comes up with undefined.

How would it be done?

Update:

this is the code I am dealing with. I guess it probably isn't the same as what I said above because I am doing it in selenium with browsermob, I just thought it would be similar to document.write(). Here is the code

var numCardsStr = selenium.getText("//div[@id='set-middle']/div[2]/h2");

var numCards = numCardsStr.substr(4,2); 

browserMob.log(numCards);

var flash = {}

for(i=0; i<(numCards); i++){

var terms = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[1]");
var defs = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[2]");

flash[terms] = defs;

browserMob.log(flash.terms);

}
+3  A: 

If you're using document.write(), there's a good chance you are trying to reference the object before it's been instantiated. My advice: don't use document.write() unless you need it in a template. For all other purposes, wait till the page loads and then run your script as an event handler.

There could be other reasons for the failure, but your code sample isn't complete enough for a diagnosis.

Robusto
i updated above. I think its a little different
chromedude
+3  A: 

EDIT: You're using two different variable names, flash and flashcards. I don't know if they are meant to be the same thing, but you are setting the value using the [] notation, then getting it using . notation.

Try:

var flash = {};

...

flash[terms] = defs;

browserMob.log(flash[terms]);

If term is a variable to represent the property you are retrieving, then you should use the square bracket notation for getting the property from the object.

Example: http://jsfiddle.net/xbMjc/ (uses alerts instead of document.write)

var object= {};

object.someProperty = 'some value';

var term = "someProperty";

document.write( object[term] );  // will output 'some value'
patrick dw
hmm.. I thought i did that but it doesnt work
chromedude
@chromedude - You have two different variable names. `flashcards` and `flash`.
patrick dw
o sorry, yeah, they are both supposed to be flash, I will change that
chromedude
I did that but it still does not work
chromedude
Have you verified the values of `term` and `def`? Try logging those to the console to see if they are what you expect.
patrick dw
I tried doing this: browserMob.log(flash[term] + "," + flash[def]); and it resulted in the term,undefined which after thinking about it made sense because I am assuming that you cannot call the second part of the object like i did. How do you call the def?
chromedude
Actually It looks like the values are correct
chromedude
@chromedude - So you logged `terms` and `def` (aside from their role in `obj`) and the values look correct. Is that right? Could you give an example of what one of the values of `terms` looks like? And you're right, you can't do `flash[def]`. You need to retrieve `def` from the object by its `terms` with `flash[terms]`.
patrick dw
The values look correct. An example terms value is "windows"
chromedude
@chromedude - Not sure what to say. If you're doing `flash[terms] = defs;` and `browserMob.log(flash[terms]);`, those are the correct way to set and get properties of an object. Not sure what role `browserMob` may have, but everything else should be good.
patrick dw
Later on in the code I have this code selenium.type("//input[@id='user-answer']", flash[term]);This is what is printing undefined.
chromedude
@chromedude - I guess it would depend on what's happening in between. The first thing I'd try is to log `flash[term]` immediately after you set its value to `defs`, and to verify that the value is present. Without seeing your code, it's hard to tell what's happening.
patrick dw
This is the rest of the code: `var term = selenium.getText("//div[@id='learn-in']/h3");``selenium.click("//input[@id='user-answer']");``selenium.type("//input[@id='user-answer']", flash[term]);`I did log `flash[terms]` and it came out with the `defs`
chromedude
@chromedude - So if `flash[term]` is giving you the correct value, then you know the `flash` object is being updated properly. So the issue must be with your `selenium` code. Unfortunately, I won't be able to help you there. I'm not sure if I even heard of selenium until this question. Sorry. :o(
patrick dw
ok, thanks for the help anyway (by the way selenium is very useful for testing websites, I really love it :) )
chromedude
@chromedude - Just watched their intro video. Looks interesting. I'll have to try it out one of these days.
patrick dw
+1  A: 
  • Avoid document.write

  • If you use Firefox, install firebug and use it's console api

  • The same console apis should work in chrome too.

  • For IE, get companion js

  • In javascript, obj.propertyname is used if the property name is known before hand. If it's not, then:

if pn contains the property name, obj[pn] should give you the value.

letronje
+2  A: 

To output the whole object as text, use a JSON library:

<script type="text/javascript" src="http://www.JSON.org/json2.js"&gt;&lt;/script&gt;

.

var o = { "term": "value" };
document.write(JSON.stringify(o, null, 4));

This will output the object as a string and indent 4 spaces to make it easy to read.


What you do is this:

var terms = "abcd";
var defs = "1234";
var flash = {};
flash[terms] = defs;

This creates this object:

{
    "abcd": "1234"
}

If you want to go through the properties (i.e. "abce"), do this:

for (var key in flash) {
    document.write('Key "' + key + '" has value "' + flash[key] + '"<br/>');
}

This will output:

Key "abcd" has value "1234"
Jason Goemaat
+1  A: 

Because I haven't seen this mentioned yet:

var a = {prop1:Math.random(), prop2:'lol'};
a.toString = function() {
    output = [];
    for(var name in this) if(this.hasOwnProperty(name) && name != 'toString') {
        output.push([name, this[name]].join(':'));
    }
    return "{\n"+output.join(",\n\t")+"\n}";
};
document.write(a);

// should look like:
/*
    {
        prop1:0.12134432,
        prop2:lol
    }
*/

In the case that you're defining an object class, like MyObj:

var MyObj = function(id) {
    this.someIdentity = id;
};
MyObj.prototype.toString = function() {
    return '<MyObject:'+this.someIdentity+'>';
};

And then anytime you write something like

document.write(new MyObject(2));

It'll appear as <MyObject: 2>.

Chris Dickinson
A: 

Well in firefox and in Chrome/Safari you could simply use...

var myObj = {id: 1, name: 'Some Name'};
window.console.log(myObj);

And the console will output something like "Object"

If you are in Chrome you could inspect the internal values of the object with ease using the built in developer console.

If you use firefox the output should come out of firebug as well...

I'm stating this as an alternative of using document.write as it seems a little bit invasive to me to output content on the document...

moQuez