NativeScript / NativeScript/android
console.log should not call object methods
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C++
- Star
- 563
- Fork
- 144
- Merge trung bình
- 10 giờ 46 phút
- Pull request đã merge (30 ngày)
- 14
Mô tả
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:

Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu tại điểm đầu vào định dạng đối tượng của console.log trong Android runtime và tái hiện các đoạn mã TypeScript được cung cấp, bao gồm cả ví dụ toJSON bị lỗi. So sánh đầu ra với hành vi của Chrome được mô tả trong issue; hoàn thành khi việc ghi log không gọi các phương thức của đối tượng và không bị dừng khi một phương thức trong số đó ném ra exception.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- android, javascript
- Lĩnh vực
- developer-experience, mobile-dev
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100