Home >Java >javaTutorial >How to implement Java function stringification

How to implement Java function stringification

WBOY
WBOYforward
2023-05-04 23:13:051357browse

JSON.stringify does not stringify functions:

Example

const person = {

name: "John",

age: function () {return 30;}

};

let myString = JSON.stringify(person);

document.getElementById("demo"). innerHTML = myString;

This can be "fixed" if the function is converted to a string before stringification.

This can be "fixed" if you convert the functions into strings before stringifying.

Example

const person = {

name: "John",

age: function () {return 30;}

};

person.age = person.age.toString();

let myString = JSON.stringify(person);

document.getElementById("demo"). innerHTML = myString;

The above is the detailed content of How to implement Java function stringification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete