I’m working on an application that uses JavaScript VERY heavily. I’m serializing JSON objects across pages, and I’m wondering if that is causing problems. If we ignore the serization, my code basically looks like this:
function MyClass() { this.init(); }
MyClass.prototype = {
init: function () {
var cd = new Date();
var ud = Date.UTC(cd.getYear(), cd.getMonth(), cd.getDate(), cd.getHours(), cd.getMinutes(), cd.getSeconds(), cd.getMilliseconds());
this.data = {
currentDateTime = new Date(ud);
}
}
}
try {
var myClassInstance = new MyClass();
alert(myClassInstance.data.currentDateTime.getFullYear());
} catch (e1) {
console.log(e1);
}
When I execute my “alert”, I get an error that says:
“Object 0112-03-14T10:20:03.206Z has no method ‘getFullYear'”
I can’t figure out why I’m getting this error. I clearly have some object. However, I anticipate that it is some typing issue. Yet, I don’t understand why. Is there a way to do a type check / cast?
Try changing this:
to this:
Inside an object literal, you need to use
:to map keys to values.