小编Slo*_*ive的帖子

如何使用包含属性名称的变量检查对象属性是否存在?

我正在检查是否存在一个对象属性,其中包含一个保存有问题的属性名称的变量.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};
Run Code Online (Sandbox Code Playgroud)

这是undefined因为它正在寻找,myObj.myProp但我希望它来检查myObj.prop

javascript object

619
推荐指数
8
解决办法
60万
查看次数

将json从文件加载到对象中

努力将json从URL上的文件(myData.json)加载到对象中,以便我可以访问属性值.

- 数据立即加载,我在应用程序中需要它很多.

- 我将在整个应用程序中访问数据,而不仅仅是在数据加载后立即发生的一个函数的一部分.

- 我确保我文件中的数据格式正确json.

按照jquery API上的示例,我不应该做一些简单的事情:

警报(jqxhr.myProperty);

并获得价值?我在这里错过了什么一步?我试过运行eval和各种各样的东西

var myObj = JSON.parse(jqxhr);

无济于事.

求求你了,谢谢你.

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON("example.json", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });
Run Code Online (Sandbox Code Playgroud)

javascript jquery json object

13
推荐指数
1
解决办法
3万
查看次数

标签 统计

javascript ×2

object ×2

jquery ×1

json ×1