3

I am new to js. I have an if condition that I did not understand. Can you tell me what this if condition does?

Object.prototype.toString.call(currentFruit) === "[object Date]"

Can you explain it? Providing my code below:

setcurrentFruit: function (fruitName, currentFruit) {
    WorklistStorage.set(fruitName, currentFruit, false);
},
getcurrentFruit: function (fruitName) {
    var currentFruit = unescapeJSON(WorklistStorage.get(fruitName, false));
    if (currentFruit == "undefined" || typeof currentFruit == "undefined" || Object.prototype.toString.call(currentFruit) === "[object Date]") {
        var date = new Date();
        currentFruit = date.toString();
        wholeQueue.setcurrentFruit(fruitName, currentFruit);
        //console.log("poppppp");
    }
    currentFruit = new Date(currentFruit);
    return currentFruit;
},
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

7

Let's break it down;

So Object.prototype.toString.call(currentFruit) is invoking the native toString of all Objects on currentFruit. This might be different to currentFruit.toString() if there is another toString defined on or inherited by currentFruit.

Object.prototype.toString returns a String of the form [object X] where X is the type of this, so comparing against [object Date] with === is asking "Is currentFruit a Date?"

Why is doing this check more useful than typeof? Because typeof will often just return "object", which is not often helpful.

What about instanceof? This will be true if the thing you're checking inherits from what you're testing against as well, so for example, x instanceof Object is usually true, which is not always helpful either.

An alternate method you could think of as similar would be to test the constructor of an Object. x.constructor === Date. This has a different set of problems, such as throwing errors if x is undefined or null so needs more checks etc. but it can be more helpful if you're working with non-native constructors, for which toString would simply give [object Object].


All this said, you need to consider whether this test will ever be true given the environment you're working with. Currently there is no standard JSON representation of a Date.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
1

Object.prototype.toString is used to get the internal [[Class]] value of javascript objects. In here, it tests whether currentFruit is a native Date object.

It could easily be replaced by currentFruit instanceof Date (though there are minor differences).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Because currentFruit is parsed JSON you can't check instanceof: http://stackoverflow.com/questions/4511705/how-to-parse-json-to-receive-a-date-object-in-javascript seems that Date doesn't exist in JSON `JSON.parse(JSON.stringify(new Date())) instanceof Date===false` – HMR Sep 19 '13 at 02:04
  • @HMR: I had expected as well that the test hardly makes sense, but notice that OP is doing `unescapeJSON` (probably with some magic) not `JSON.parse`. – Bergi Sep 19 '13 at 02:29