19

I'm getting a "Object doesn't support this property or method error", does anyone know why?

I do have values plugged into userId, fname, lname, oname, sam, hasAccess

function Employee(id, fname, lname, oname, sam, access) {
    this.id = id;
    this.fname = fname;
    this.lname = lname;
    this.oname = oname
    this.sam = sam;
    this.access = access;
}

var emp = new Employee(userId, fname, lname, oname, sam, hasAccess);

var jsonstuff = emp.toSource(); //Breaking here

Although this link says its possible http://www.w3schools.com/jsref/jsref_toSource_date.asp

Cyclonecode
  • 26,179
  • 11
  • 66
  • 86

5 Answers5

24

toSource() does not work in Internet Explorer or Safari. It is Gecko-only. See Implementing Mozilla's toSource() method in Internet Explorer for alternatives.

Community
  • 1
  • 1
John Kugelman
  • 307,513
  • 65
  • 473
  • 519
  • 9
    +1 for mentioning Safari! The browser world isn't just Firefox and IE. – Steve Harrison Jul 09 '09 at 03:00
  • @Vince: If your boss is that incompetent, try to explain to him that making sure that it works outside IE and FF will result in less maintenance work next time IE is upgraded. It will also make the IE and FF users much less annoyed with your company when they browse the site from their cell phone (no matter if it is an iPhone, a Nokia or something else without IE). – Fredrik Jul 09 '09 at 08:14
  • Don't forget Chrome is really getting into the fight. Leaving Safari+Chrome+Opera aside, you could be leaving 15% of the share (depending on your website market). Like an example, see: http://www.w3schools.com/browsers/browsers_stats.asp – lepe Feb 15 '10 at 02:17
  • @Vince: 1998 called, it wants its rhetoric back. Safari and Chrome share the Webkit rendering engine; together, it's a considerable share of the market. Also, if it works in FF, it's 90% chance it'll work in Safari,Chrome,Opera,etc. (not in IE though, that's more like a 50% chance) – Piskvor left the building Jun 17 '10 at 20:44
9

Try using a JSON serializer instead. toSource is Mozilla specific and not supported by IE.

If you are just debugging then your best bet is going to be to install Firebug and use console.dir(emp); to print the contents of an object to the console window.

Update: Just notice that on the link you posted it says, "Note: This method does not work in Internet Explorer!" And on the MDC page it says "Non-Standard".

alecxe
  • 414,977
  • 106
  • 935
  • 1,083
Prestaul
  • 76,622
  • 10
  • 80
  • 84
5

You can either call toString instead, or put in a condition like this...

var jsonstuff = (emp.toSource) ? emp.toSource() : emp.toString();

EDIT:

Since this is not working for you, you might want to use JSON.stringify()

Josh Stodola
  • 77,975
  • 43
  • 178
  • 222
3

Whilst not recommended (to extend native JS Objects), during development you could use:

Object.prototype.toSource 
    || (Object.prototype.toSource = function(){return JSON.stringify(this);})

c = {a:100}
//>Object
c.toSource()
//>"{"a":100}"

cheers!

Lorenz Lo Sauer
  • 20,692
  • 12
  • 75
  • 85
1

I suggest using an existing library or plugin:

Justin Johnson
  • 29,495
  • 7
  • 60
  • 86