2

If I have this code

var node = function(n) {

    var name = n;
    var children = [];
    var finished = false;
    var failed = false;

    this.getName = function() {
        return name
    };
    this.downloadData = function(obj) {

    };

    this.getChildren = function() {
        return children;
    };
    this.setChildren = function(c) {
        Array.prototype.push.apply(children, c);
    };
    this.isFinished = function() {
        return finished;
    };
    this.setFinished = function() {
        finished = true;
    }
    this.isFailed = function() {
        return failed;
    }
    this.setFailed = function() {
        failed = true;
    }
};

How can I convert this into an object like:

var a = new node("a");
var j = JSON.stringify(a);

result

{"name":"a","children":[],"finished":false,"failed":false}

thanks

2
  • Fortunately it's not possible. Commented Mar 24, 2015 at 20:44
  • I think the title is poorly chosen. It sounds like you want to convert a function to a string, where you seem to want to convert an object created via new ..... to convert to JSON. FYI, it doesn't matter where the object came from. Commented Mar 24, 2015 at 20:48

2 Answers 2

4

This could be done by implementing the toJSON function.

If an object being stringified has a property named toJSON whose value is a function, then the toJSON() method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON() method when called will be serialized. - Mozilla

eg:

var node = function(n) {

  var name = n;
  var children = [];
  var finished = false;
  var failed = false;

  this.toJson = function toJson() {
    return {"name":name ... };
  } 

 }
Sign up to request clarification or add additional context in comments.

3 Comments

but what do u call on "children"?
That depends on what the children are, your question just indicates that its an array.
children can contain more nodes.
2

You need object properties instead of variables.

So, instead of declaring var name = n;, you would declare this.name = n;. Which would make it look something like

var node = function(n) {
    this.name = n;
    this.children = [];
    this.finished = false;
    this.failed = false;
    ///other functions here
}

2 Comments

This would change the access modifiers of the variables from being private/local to public. Might not be what OP intended them to be, seing he/she has created get/set for them.
Ah, that's an interesting point. In which case, your solution is definitely the way to go. If the the end result was a flat JSON object, I'm wondering why the getters/setters would be necessary. You could not use them when the JSON string was parsed later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.