Javascript toSource()方法不起作用

17 javascript

我得到一个"对象不支持此属性或方法错误",有谁知道为什么?

我确实将值插入到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
Run Code Online (Sandbox Code Playgroud)

虽然这个链接说它可能是http://www.w3schools.com/jsref/jsref_toSource_date.asp

Joh*_*ica 22

toSource()在Internet Explorer或Safari中不起作用.它只是Gecko.请参阅在Internet Explorer中实现Mozilla的toSource()方法以获取替代方法.

  • +1提及Safari!浏览器世界不仅仅是Firefox和IE. (9认同)

Pre*_*aul 9

请尝试使用JSON序列化程序. toSource是Mozilla特定的,IE不支持.

如果你只是调试那么你最好的选择是安装Firebug并使用console.dir(emp); 将对象的内容打印到控制台窗口.

更新:请注意,在您发布链接上说:"注意:此方法在Internet Explorer中不起作用!" 在MDC页面上,它显示"非标准".

  • 试试`alert(JSON.stringify(obj))` (3认同)

Jos*_*ola 5

你可以改为调用toString,或者放入这样的条件......

var jsonstuff = (emp.toSource) ? emp.toSource() : emp.toString();
Run Code Online (Sandbox Code Playgroud)

编辑:

由于这不适合您,您可能希望使用JSON.stringify()