While learning the mongoDB I notice one constructor returning string with prototype function. For example
Following code will generate a random string, first 4 letter from string contain timestamp and to get the time stamp it has the prototype function.
const id = new ObjectID() // 5c8766f4bcd712911ab81eea
console.log(id.getTimestamp()) // will print the date
I was just working how it been done. I am trying to create the simple constructor in same way but it always return object. How can we make it to return single value instead of object with prototype function.
function getMyTime(){
this.timestamp = new Date().getTime()
}
const timestamp = new getMyTime(); // should print timestamp 1552385374255
console.log(timestamp.printReadableDate()) // should print date Tue Mar 12 2019 15:39:34 GMT+0530 (India Standard Time)
Edited
class GetTime {
constructor(){
this.time = new Date().getTime();
}
getActualDate(){
}
toString(){
return this.time
}
}
const stamp = new GetTime();
console.log(stamp) // should be 1552472230797
console.log(stamp.getActualDate()) //should be Wed Mar 13 2019 15:47:10 GMT+0530 (India Standard Time)
from return standalone value from constructor not object
0 komentar:
Posting Komentar