网站的JSON响应的一部分有这个(...为上下文添加):
{..., now:function(){return(new Date).getTime()}, ...}
Run Code Online (Sandbox Code Playgroud)
向JSON添加匿名函数是否有效?我希望每次你访问"时间"返回一个不同的值.
如何在警报框中检查对象?通常警告对象只会抛出节点名称:
alert(document);
Run Code Online (Sandbox Code Playgroud)
但我想在警告框中获取对象的属性和方法.如果可能,我该如何实现此功能?或者还有其他建议吗?
特别是,我正在寻找一个生产环境的解决方案,其中console.log和Firebug不可用.
我有一个包含循环引用的JavaScript对象定义:它有一个引用父对象的属性.
它还具有我不想传递给服务器的功能.我如何序列化和反序列化这些对象?
我读过这样做的最好方法是使用Douglas Crockford的stringify.但是,我在Chrome中收到以下错误:
TypeError:将循环结构转换为JSON
代码:
function finger(xid, xparent){
this.id = xid;
this.xparent;
//other attributes
}
function arm(xid, xparent){
this.id = xid;
this.parent = xparent;
this.fingers = [];
//other attributes
this.moveArm = function() {
//moveArm function details - not included in this testcase
alert("moveArm Executed");
}
}
function person(xid, xparent, xname){
this.id = xid;
this.parent = xparent;
this.name = xname
this.arms = []
this.createArms = function () {
this.arms[this.arms.length] = new arm(this.id, this);
}
}
function group(xid, xparent){
this.id = …Run Code Online (Sandbox Code Playgroud) JSON.stringify(eventObject);
得到:
TypeError: Converting circular structure to JSON
dojox.json.ref.toJson(eventObject);
得到:
TypeError: Accessing selectionEnd on an input element that cannot have a selection.
是否有一些库/代码可以用来完成它?
出于某种原因,我似乎无法在DOMWindow对象上使用JSON.stringify.例如:
console.log(window.self); // Outputs a hierarchical DOMWindow object
console.log(JSON.stringify(window.self)); // Outputs nothing - not even an error
alert(window.self); // Alerts "[object DOMWindow]"
alert(JSON.stringify(window.self)); // Again nothing - not even an error
Run Code Online (Sandbox Code Playgroud)
在Safari和Chrome上测试过.有没有人有任何想法我怎么能做到这一点?
编辑:
将编辑移至新问题,因为它并不特定于此.
我正在尝试存储HTML标记的引用,以供以后重用。
例如,如果我单击一个div并在Javascript中保存一个指向该div的指针,是否有一种方法可以序列化此类指针?所以我可以反序列化它,并在Web应用程序的另一个实例中使用指针?
我只能想到的方法如下:
使用ID或名称属性
为该元素创建一个CSS选择器
还有其他想法吗?=)
如果我做:
var el =
{
o : document.createElement("iframe")
}
var fs = JSON.stringify(el);
and then I try to access with
var ofs = JSON.parse(fs);
Run Code Online (Sandbox Code Playgroud)
ofs.o 包含一个空对象而不是iframe元素为什么?
我正在创建一个jquery对象,如下所示:
htmlCode = $("<div id='"+data.layoutDescriptions[i].id+"'></div");
Run Code Online (Sandbox Code Playgroud)
它主要是在我这样做时似乎缺少一些元素:
if(data.type == "ListView"){
htmlCode.append("<SELECT property='"+ data.property +"' size="+ data.listProps.length +" multiple>");
i = 0;
while(i < data.listProps.length){
htmlCode.append("<OPTION value='"+ i+1 +"'>"+ data.listProps[i] +"</OPTION>");
i++;
}
htmlCode.append("</SELECT>");
}
Run Code Online (Sandbox Code Playgroud)
其中data是Json对象.
当我这样做它作为一个字符串它是有效的.例如,而不是
htmlCode.append("<OPTION value='"+ i+1 +"'>"+ data.listProps[i] +"</OPTION>");
Run Code Online (Sandbox Code Playgroud)
我会做
htmlCode = htmlCode + "<OPTION value='"+ i+1 +"'>"+ data.listProps[i] +"</OPTION>";
Run Code Online (Sandbox Code Playgroud)
我想找出遗漏的内容,所以我想看看对象我已经尝试过以下内容:
alert(JQuery.htmlCode.stringify());
alert(htmlCode.html);
Run Code Online (Sandbox Code Playgroud)
更虚空的工作.
有任何想法吗??
谢谢.