如何访问匿名对象的属性?

时间:2022-12-30 07:21:40
$.post("test.php", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  });

The object { name: "John", time: "2pm" } is anonymous. Normally, I would access the properties of an object using syntax similar to the following:

对象{name:“John”,time:“2pm”}是匿名的。通常,我会使用类似于以下的语法访问对象的属性:

objectname.propertyname

But what can I do when there is no objectname? How can I access propertyname?

但是,如果没有对象名,我该怎么办?我如何访问propertyname?

1 个解决方案

#1


The whole point of an anonymous object is that it is just that, anonymous. It is accessed in context only. If you want to access the object later on, then you need to assign the object to a variable.

匿名对象的重点在于,匿名对象就是这样。它仅在上下文中访问。如果以后要访问该对象,则需要将该对象分配给变量。

Try:

var obj = { name: "John", time: "2pm" };
$.post("test.php", obj,
  function(data){
    alert("Data Loaded: " + data);
    alert("obj name is " + obj.name);
  });

#1


The whole point of an anonymous object is that it is just that, anonymous. It is accessed in context only. If you want to access the object later on, then you need to assign the object to a variable.

匿名对象的重点在于,匿名对象就是这样。它仅在上下文中访问。如果以后要访问该对象,则需要将该对象分配给变量。

Try:

var obj = { name: "John", time: "2pm" };
$.post("test.php", obj,
  function(data){
    alert("Data Loaded: " + data);
    alert("obj name is " + obj.name);
  });