I can easily create and object in a lot of different. For example like this:
var myObject = {
myFunction: function () {
return "";
}
};
So if I alert this object I get something this:
[object Object]
Now I’m if I alert the NaN object I get this:
NaN
And I can still call function from it:
alert(NaN.hasOwnProperty().toString());
So my question is how can I create a object like the NaN object?
- If I alert it, it should return something like
myObject - I should be able to create and call functions on it like
myObject.myFunction();
Only objects that are created using a constructor function get their class name displayed using
console.log(), as shown in @Esalijia’s answer.Even then, it’ll be the class name that’s displayed, not the name of whatever variable you happened to assign the object to.
Adding a
toString()method to the object will however change the output of thealert()function, since that always looks for the presence of that method and use it if it exists.So, writing:
will show
booin the alert box, instead of[Object object].