NativeScript / NativeScript/android
console.log should not call object methods
Nessuno ha ancora preso questa issue.
- Lingua principale
- C++
- Stelle
- 563
- Fork
- 144
- Merge medio
- 10h 46m
- PR unite (30g)
- 14
Descrizione
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:

Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dal punto di ingresso della formattazione degli oggetti di console.log nell’Android runtime e riproduci gli snippet TypeScript forniti, incluso l’esempio toJSON che fallisce. Confronta l’output con il comportamento di Chrome descritto nell’issue; il lavoro è completato quando il logging non invoca i metodi degli oggetti e non si interrompe quando uno di essi genera un’eccezione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- android, javascript
- Ambito
- developer-experience, mobile-dev
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100