简体   繁体   English

提醒对象

[英]alert the object

I am setting an object like this 我正在设置这样的对象

n.name = n.name.join(String.fromCharCode(255));
n.description = n.description.join(String.fromCharCode(255));

I want to be able to alert(n); 我希望能够alert(n); but it tells me [Object] 但它告诉我[Object]

is there a way to alert complete object? 有没有办法提醒完整的物体?

thanks 谢谢

Javascript supports adding a toString() function to your objects. Javascript支持向您的对象添加toString()函数。 This will get called when you alert your object. 当您警告对象时,将调用此方法。 For example: 例如:

n.toString = function(){
    return this.name + '\n' + this.description;
}

then alert(n); 然后警报(n); will display whatever content your function specifies. 将显示函数指定的任何内容。

I like the var_dump in php, so I often use a function like this to dump variables 我喜欢php中的var_dump ,所以我经常使用这样的函数来转储变量

function var_dump(object, returnString)
{
  var returning = '';
  for(var element in object)
  {
    var elem = object[element];
    if(typeof elem == 'object')
    {
      elem = var_dump(object[element], true);
    }
    returning += element + ': ' + elem + '\n';
  }
  if(returning == '')
  {
    returning = 'Empty object';
  }
  if(returnString === true)
  {
    return returning;
  }
  alert(returning);
}

I was asking the same kind of question as Autolycus today. 我今天问的是与Autolycus相同的问题。 I'm using jqGrid and I wanted to see what the object it created actually was. 我正在使用jqGrid,我想看看它实际创建的对象是什么。 I didn't create the object and I wanted to see what it looked like. 我没有创建对象,而是想查看它的外观。 I know it's probably old school, but I still use alerts in javascript for some of my debugging (though I agree FireFox & Firebug are the way to go for most things). 我知道这可能是老学校了,但是我仍然在一些调试中使用javascript中的警报(尽管我同意FireFox和Firebug是大多数事情的解决之道)。

I found an answer to what I was looking for here: http://javascript.internet.com/debug-guide.html , which is unbelievably old. 我在这里找到了想要的答案: http : //javascript.internet.com/debug-guide.html ,这太老了。

But I tweaked it to give me what I needed and since I think it answers Autolycus's question in a new way and I think someone else might be looking here, like me, for this someday, here it is: 但是我对其进行了调整,以提供所需的功能,因为我认为它以新的方式回答了Autolycus的问题,而且我认为有一天,像我一样,其他人可能会在这里看到以下内容:

obj = n;
var temp = "";
for (x in obj) {
    temp += x + ": " + obj[x] + "\n";
}
alert (temp);

I apologize in advance if answering an old question is breaking some kind of rule. 如果您回答一个老问题违反了某种规则,我事先表示歉意。

all best, ember 最好的,余烬

There are a couple of alternatives: 有两种选择:

1. Use http://www.gscottolson.com/blackbirdjs/
2. Use console.log() that comes with Firebug, but requires Firefox (even if you only target only IEs, it's still wise to use Firefox and Firebug as aprt of testing of your web app development)

It depends what you mean by alerting the complete object. 它取决于警告整个对象的含义。

You can't really just output every object as a string and have it make sense. 您不能真正将每个对象都输出为字符串并使它有意义。 To define how an object will display itself as a string we use the .toString(); 为了定义对象如何显示为字符串,我们使用.toString();。 method. 方法。

So try alert(n.toString()); 因此,请尝试alert(n.toString()); and see if that will give you what you want. 看看是否能满足您的需求。 If the object is your own, then define the toString(); 如果对象是您自己的,则定义toString(); and have it return a string of the parameters and fields that you want to output. 并使其返回要输出的参数和字段的字符串。

Something like... 就像是...

alert(n.name); 

...I think is what you want. ...我想是你想要的。

If you are trying to debug, you would be better suited to using FireFox/Firebug instead of inserting a load of alerts(); 如果您要调试,则最好使用FireFox / Firebug,而不要插入很多alert();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM