简体   繁体   English

Console.log 不输出 json

[英]Console.log not outputting json

 var _json = [{"codigo": "1", "nome": "Robert Santos", "turma": "EM2A"}, {"codigo": "2", "nome": "Eduardo Alves Silveira", "turma": "EM3A"}, {"codigo": "3", "nome": "Amara Gouveia", "turma": "EMT2B"}, {"codigo": "4", "nome": "Tainá Belchior da Silva", "turma": "EF5B"}]; function console() { console.log(JSON.stringify(_json, null, 4)); } console();

Hi, i'm using this code, but when I try execute console() , i'm getting this error: TypeError: console.log is not a function嗨,我正在使用这段代码,但是当我尝试执行console()时,出现了这个错误: TypeError: console.log is not a function

What should I do to fix this problem?我应该怎么做才能解决这个问题?

You're getting TypeError: console.log is not a function because you're overwriting the console object in the global scope with your own function called console which doesn't have a log function.你得到TypeError: console.log is not a function因为你用你自己的函数覆盖全局范围内的console对象,称为console ,它没有log功能。 You can solve this issue by renaming your function to something else, snippet example:您可以通过将函数重命名为其他名称来解决此问题,代码段示例:

 var _json = [{"codigo": "1","nome": "Robert Santos","turma": "EM2A"}, {"codigo": "2","nome": "Eduardo Alves Silveira","turma": "EM3A"},{"codigo": "3","nome": "Amara Gouveia","turma": "EMT2B"},{"codigo": "4","nome": "Tainá Belchior da Silva","turma": "EF5B"}]; function log() { console.log(JSON.stringify(_json, null, 4)); } log();

The global window object already has a function named console , which you are trying to override with your own console function.全局window对象已经有一个名为console的函数,您正试图用您自己的console函数覆盖它。 Just rename your console function to something else.只需将您的console功能重命名为其他名称即可。

You cannot name your function console你不能命名你的功能console

Reason - Console is an existing object in JavaScript that provides access to the browser's debugging console. Reason - Console是 JavaScript 中的现有对象,它提供对浏览器调试控制台的访问。 Read MDN web docs to know more about it阅读MDN 网络文档以了解更多信息

Corrected Code更正代码

 var _json = [{"codigo": "1","nome": "Robert Santos","turma": "EM2A"}, {"codigo": "2","nome": "Eduardo Alves Silveira","turma": "EM3A"},{"codigo": "3","nome": "Amara Gouveia","turma": "EMT2B"},{"codigo": "4","nome": "Tainá Belchior da Silva","turma": "EF5B"}]; function logger() { console.log(JSON.stringify(_json, null, 4)); } logger();

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

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