Json string automatically parsed on function argument?


In Main process:

var project = new Project() // return {a:12, b:13}
broswerWindow.webContents.on('did-finish-load', function(){

In browserWindow.js

function init(project){
console.log(project); // it prints an Object

How? I think it should be string because of JSON.stringify


executeJavascript expects a string, ${JSON.stringify(project)} is giving you a string of the object representation, which is being re-evaluated as an object.

Try wrapping the stringify in ':



“being re-evaluated” by who?

by the javascript engine because of the language standard or by chromium ?

PS: this actually an advantage because we can’t pass object directly to executeJavascript() or can we?


I’ll start with your PS, you can’t because it’s expects a string, and this is a way around it.

So what you’re doing is building a string (JSON.stringify(project)), and then putting it into a string init(foobar). This string is then being evaluated as Javascript, meaning the stringified JSON is being understood as a Javascript object, because it’s not wrapped in any ' to mark it as a string.

It’s being evaluated by the executeJavascript(), that’s what it’s doing, it’s interpreting a string, which happens to have a string representation of an object inside.


ah so electron does the re-evaluation behind the scene… thanks for explaining :slight_smile:


I mean, not really, that’s just what the function does. It’s not Electron trying to do anything helpful, it’s a question of what your input is.

So with your template string, you’re turning your JSON into a string, and dumping that string into the rest of the string, so you have one big string with some JSON in it.

executeJavascript reads the string and converts it back to Javascript, because the JSON string isn’t wrapped in quotes (demarking it as a string), it’s treated as an object and not the string you’re expected.

Think about it like this, if you didn’t stringify, you’d be running init using the string [object Object], because that’s how objects are evaluated when using toString. It’s a case of delimitation, like I said before, if you wrapped it in ', the function would recognise your JSON blob as a string and not an object and treat it as you were expecting.


what I mean with electron is the function it has…

anyway, based on your recent explaination

is this how it does the evaluation:

project = {a:1, b:2}

init({a: 1, b:2})



Yeah, so:

project = {a:1, b:2}

init({a: 1, b:2})

init([object Object])

init('{a: 1, b:2}') //and therefore a string, not an object