简体   繁体   中英

Copy Date object without old data

I'm trying to write custom deep clone functionality and I have some problem with Date object. for example let now = {time: new Date()} or let now = {data: new Date().getDay()} , and I want copy it, I don't need to copy current time, I only need to copy new Date() constructor, or new Date().getDay() functionality, for example, if i will run the originally object now, and after few days I will run the copy object, I don't need a same result, I need the recently get new Date() function result.

You can only get at the object as it were if you scan the source

Here using DOM as an example

 const script = [...document.querySelectorAll("script")].filter(scr => scr.innerText.match(/let\s+now/)) if (script.length) console.log(script[0].textContent.trim())
 <script> let time = {data: new Date().getHours()} console.log(JSON.parse(JSON.stringify(time))) </script> <script> let now = {data: new Date().getDay()} </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM