Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does a show "function getFullYear() { [native code for Date.getFullYear, arity=0] }" rather than the value returned by the method?

In the Google Apps Script Editor I have the following code

  function t(){
    var d = new Date;
    Logger.log(d);
    var y = d.getFullYear;
    Logger.log(y);
    if (y == 2013) {
      Logger.log("yes");
    } else {
      Logger.log("No");
    }
  }

I see the following results when viewing the log.

[13-06-23 19:53:52:863 PDT] Sun Jun 23 19:53:52 GMT-07:00 2013
[13-06-23 19:53:52:864 PDT] function getFullYear() { [native code for Date.getFullYear, arity=0] }

[13-06-23 19:53:52:864 PDT] No

I thought I would see an integer or perhaps a string that is the value returned by getFullYear.

Whatever is causing this problem for me is not unique to this method.

I am sure this is pretty basic.

Thanks in advance.

like image 728
WesR Avatar asked Dec 21 '22 04:12

WesR


1 Answers

This stores a reference to the getFullYear function in y:

var y = d.getFullYear;

This calls the getFullYear function and stores the result of the function call in y:

var y = d.getFullYear();
// ------------------^^

Perhaps you're confused because you can create objects with or without the parentheses (assuming you don't need to pass any arguments of course):

var d1 = new Date;
var d2 = new Date();

Both of those do the same thing but that's just a quirk/feature of the new operator.

like image 163
mu is too short Avatar answered Dec 28 '22 07:12

mu is too short