NativeScript / NativeScript/android
console.log should not call object methods
还没有人认领这个 Issue。
- 主要语言
- C++
- 星标
- 563
- 派生
- 144
- 平均合并
- 10 小时 46 分钟
- 30 天内合并 PR
- 14
描述
Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):
- CLI: 8.1.5
- Cross-platform modules: 6.5.11
- Android Runtime: 6.5.3
- iOS Runtime (if applicable): Possibly irrelevant
- Plugin(s): Possibly irrelevant
Describe the bug
When an object contains a toString or toJSON method, and you print that object through console.log, it will automatically call that function. This is very different from what Chrome console.log does and may cause confusing behavior. For example:
The NativeScript HTTP client returns an object with a property called content that contains an object with a to toJSON method, when you console.log the return object, it will automatically call that toJSON method, while it might not even be JSON, causing the console.log to fail with a weird error from JSON.parse, like Unexpected end of JSON input.
To Reproduce
const randomVar = "test123";
const obj = {
toJSON: () => "toJSON => this should not be logged " + randomVar
};
console.log(obj);
Expected behavior
I think it should print something like:
{
"toJSON": ƒ
}
Or, it should catch any errors that happen during the format, and then fall back to it's normal behavior (going to toString, or not printing the property at all). It should not be breaking off the whole console.log and return the exception.
Additional context
Output when doing something like this in Chrome:

Output when doing something like this in NS:

When causing an error inside the function with:
const obj = {
toJSON: () => JSON.parse("fakejson")
};
console.log(obj);
In Chrome:

In NS:

贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Android runtime 的 console.log 对象格式化入口开始,复现提供的 TypeScript 代码片段,包括失败的 toJSON 示例。将输出与 issue 中描述的 Chrome 行为进行比较;当日志记录不会调用对象方法,也不会在其中一个方法抛出异常时中止时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- android, javascript
- 领域
- developer-experience, mobile-dev
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100