Home > front end >  JS function serialization and deserialization
JS function serialization and deserialization

Time:12-01

Everybody, I met such a problem, want to you to ask,
On the first code, as shown in the following:
 



<meta charset="utf-8" & gt;
<meta HTTP - equiv="X - UA - Compatible" content="IE=edge" & gt;
<meta name="viewport" content="width=device - width, initial - scale=1.0" & gt;
JS function serialization and deserialization of problem & lt;/title> <br/><script> <br/>Let animal={<br/>Name: "cat," <br/>Color: white, <br/>Age: 3, <br/>The run: the function () {<br/>Document. Writeln (" the cat is running. "); <br/>} <br/>}; <br/>Animal. The run (); <br/>Let the json=json. Stringify (animal); <br/>Document. Writeln (" & lt; br>" ); <br/>Document. Writeln (json); <br/></script> <br/></head> <br/><br/><body> <br/></body> <br/><br/></html> <br/></pre> <br/>As shown in the code, are included in the definition of the animal a run () method, serialization, disappeared, the content of the output results as follows: <br/><pre> <br/>The cat is running. <br/>{" name ":" cat ", "color" : "white", "age" : 3} <br/></pre> <br/>I want to achieve effect is: <br/>1, animal object serialized string contains a run method and its code, <br/>2, after deserialization strings that will be serialized, the run () method can also be normal execution, <br/>First of all, thank you!<p class="article - content rp"> CodePudding user response: </p>Please people, under the guidance of great god answer, please. Thank you!<p class="article - content rp"> CodePudding user response: </p>Json data already cannot function, <br/>Unless the function into a string, the deserialization and then use the string to create a new function, <br/>So as long as the function of using the function outside variables should be no problem in the local scope, <br/><p class="article - content rp"> CodePudding user response: </p><pre> <br/>Let animal={<br/>Name: "cat," <br/>Color: white, <br/>Age: 3, <br/>The run: the function () {<br/>Document. Writeln (" the cat is running. "); <br/>} <br/>}; <br/>Animal. The run (); <br/>Let the json=json. Stringify (animal, function (n, v) {<br/>If (typeof v=="function") <br/>Return v.t oString (); <br/>Return v. <br/>}); <br/>Document. Writeln (" & lt; br>" ); <br/>Document. Writeln (json); <br/>Document. Writeln (" & lt; br>" ); <br/>Let obj=JSON. Parse (JSON, function (n, v) {<br/>If (typeof v=="string" & amp; & V.i ndexOf (" function ")==0) <br/>The return window. Eval (' (' + v + ') '); <br/>Return v. <br/>}); <br/>Obj. The run (); <br/></pre> <br/><p class="article - content rp"> CodePudding user response: </p><fieldset> <legend> reference 3 floor sky waves reply: <legend> <blockquote> <pre> <br/>Let animal={<br/>Name: "cat," <br/>Color: white, <br/>Age: 3, <br/>The run: the function () {<br/>Document. Writeln (" the cat is running. "); <br/>} <br/>}; <br/>Animal. The run (); <br/>Let the json=json. Stringify (animal, function (n, v) {<br/>If (typeof v=="function") <br/>Return v.t oString (); <br/>Return v. <br/>}); <br/>Document. Writeln (" & lt; br>" ); <br/>Document. Writeln (json); <br/>Document. Writeln (" & lt; br>" ); <br/>Let obj=JSON. Parse (JSON, function (n, v) {<br/>If (typeof v=="string" & amp; & V.i ndexOf (" function ")==0) <br/>The return window. Eval (' (' + v + ') '); <br/>Return v. <br/>}); <br/>Obj. The run (); <br/></pre> </blockquote> <fieldset> thank you, I try what you have said this way <img SRC="https://img.codepudding.com/202012/172829010327541.gif" Alt=""/> <img SRC=" https://img.codepudding.com/202012/172829010327541.gif "Alt=" "/> <img SRC=" https://img.codepudding.com/202012/172829010327541.gif "Alt=" "/> </div> <div class="th_page th_page_color"></div> <div class="umCopyright"> <p>Page link:<a href="/frontend/90483.html" target="_blank" style="color:#999">https//www.codepudding.com/frontend/90483.html</a></p> </div> <div class="detail-arr"> <div class="detail-arr-left">Prev:<a href='/frontend/90482.html'>Vue how in v - the use of the model of dynamic will index after string concatenation binding the id</a></div> <div class="detail-arr-right">Next:<a href='/frontend/90484.html'>A question about json parsing</a></div> </div> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="hot-tags neitags"> <ul> <li><i class="iconfont icon-x-tags"></i> Tags:  </li> <a href='/e/tags/?tagname=JavaScript' target='_blank'>JavaScript</a> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="xiangguan"> <ul class="msg msghead"> <li class="tbname">Related</li> </ul> <ul> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="flinks"> <ul> <li><i class="iconfont icon-x-tags"></i> Links:  </li> <li class="liflinks"><a target="_blank" href="/" title="CodePudding">CodePudding</a></li> </ul> </div> </div> </div> </div> <div class="footer"> <p><span style="font-size:16px;color:#666;font-weight: bold">About Us:</span>  <a href="https://www.codepudding.com/contact.html">Contact Us</a>      <a href="https://www.codepudding.com/service.html">Terms of Service</a>      <a href="https://www.codepudding.com/privacy.html"> Privacy Policy</a></p> <p class="foot_info">Copyright © 2010-2023,Powered By <a href="/" target="_blank">CodePudding</a> </p> </div> <script type="text/javascript" src="/skin/code/tianhu.js"></script> </body></html>