简体   繁体   English

如何在警报中显示 console.log 输出

[英]How do I show console.log output in an alert

If I create an object and use console.log it will expand the object and I can see the object properties.如果我创建一个对象并使用 console.log,它将展开该对象并且我可以看到该对象的属性。 If I use alert it does not.如果我使用警报,它不会。 Is there any way to do the same thing in an alert?有没有办法在警报中做同样的事情?

    car=new Object();
    car.wheels=4;
    car.make="honda";
    car.owner="me"

    console.log(car); 

output: Object { wheels: 4, make: "honda", owner: "me" }输出:对象 { wheels: 4, make: "honda", owner: "me" }

alert(car)

output is [object Object]输出是 [object Object]

How can I get the same output for the alert?如何获得相同的警报输出?

As others have stated you use JSON.stringify .正如其他人所说,您使用JSON.stringify

But I wanted to give a few other pointers.但我想给出其他几点建议。 I don't know if you know these already, but your example indicated that you might appreciate the tips.我不知道您是否已经知道这些,但是您的示例表明您可能会喜欢这些提示。

  1. In JavaScript it is recommended to not use new Object() Instead just create your object:在 JavaScript 中,建议不要使用new Object()而只是创建你的对象:

 var car = { wheels: 4, make: "honda", owner: "me" }

  1. Always use var , let or const when creating variables.创建变量时始终使用varletconst If you don't then they are created on the global scope.如果您不这样做,那么它们将在全球范围内创建。 In the browser you would be creating the new variables on the window object.在浏览器中,您将在window对象上创建新变量。

  2. JSON.stringify has other parameters: JSON.stringify还有其他参数:

JSON.stringify(value[, replacer[, space]])

The replacer is not often used, but it can provide a way of filtering and preventing recursive data. replacer器不经常使用,但它可以提供一种过滤和防止递归数据的方法。

The space parameter can be a number form 0 to 10 or a number of characters in a string from 0 to 10. This indicates how much to indent each level in the output. space参数可以是 0 到 10 之间的数字或 0 到 10 之间的字符串中的字符数。这表示输出中每个级别的缩进量。

 function replacer(key, value) { if (typeof value === 'string') { return undefined; } return value; } var foo = { company: 'Sony', model: 'Playstation', version: 4, pricing: [ { pro: false, price: 299.00 }, { pro: true, price: 399.00 } ], comments: 'none' }; alert(JSON.stringify(foo, replacer, 2)); alert(JSON.stringify(foo, replacer, '----'));

You can use this alert(JSON.stringify(object))你可以使用这个alert(JSON.stringify(object))

In the console log method, the parameter is considered as an object.在控制台日志方法中,参数被认为是一个对象。 So, the object can be in any form like an array, string, integer, etc., and We will get the content.因此,对象可以是数组、字符串、整数等任何形式,我们将获取内容。 But in the alert method, it accepts only the string.但是在 alert 方法中,它只接受字符串。 So, if you send the object, it will convert it as string version of the object ( object Object ).因此,如果您发送对象,它会将其转换为对象的字符串版本 (object Object)。 If you stringify the object and send as a parameter to the alert method, it will display the content.如果您将对象字符串化并作为参数发送给警报方法,它将显示内容。 Try this one,试试这个,

window.alert(JSON.stringify(object));

If I create an object and use console.log it will expand the object and I can see the object properties.如果我创建一个对象并使用 console.log 它将展开该对象并且我可以看到对象属性。 If I use alert it does not.如果我使用警报,则不会。 Is there any way to do the same thing in an alert?有没有办法在警报中做同样的事情?

    car=new Object();
    car.wheels=4;
    car.make="honda";
    car.owner="me"

    console.log(car); 

output: Object { wheels: 4, make: "honda", owner: "me" }输出:对象{轮子:4,制造:“本田”,所有者:“我”}

alert(car)

output is [object Object]输出是[对象对象]

How can I get the same output for the alert?如何获得相同的警报输出?

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

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