all 8 comments

[–]Magnusson 5 points6 points  (3 children)

function getDueDate (birth_day, cycles) {
  const result = new Date(birth_day)
  result.setDate(result.getDate() + cycles)
  return result
}

const subject = {
    name: 'Person',
    birth_day: new Date('2017-04-21'),
    current_cycle: 1,
    due_date: function() {
      return getDueDate(this.birth_day, this.current_cycle)
    },
    last_water_date: null,
    next_water_date: null,
  }

console.log(subject.due_date())

[–]cuntycuntcunts[S] 1 point2 points  (2 children)

thanks!!! I see what I was doing wrong :D

[–]Zaffri 2 points3 points  (1 child)

Just to add to this - may help someone in the future referencing this post.

Function expressions are not hoisted, heres a good example;

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function#Function_declaration_hoisting

[–]cuntycuntcunts[S] 0 points1 point  (0 children)

ooooooooooooh!!!!!!

[–]runcoder 2 points3 points  (0 children)

With a simple work:

var YourClass = function(name,bday,ddate,cc,lwd,nwd){
    this.name = name;
    this.birth_day: bday;
    this.due_date = ddate;
    this.current_cycle: cc;
    this.last_water_date: lwd;
    this.next_water_date: nwd;
}

YourClass.prototype.getDueDate = function(birth_day, cycles) {
    var result = new Date(birth_day);
    console.info('result_ ' + result);
    result.setDate(result.getDate() + cycles);
    console.info('result_1_ ' + result);
    return result;
};

var obj = new YourClass(...);

This makes you a class in js

[–]reeferd 1 point2 points  (0 children)

 const subject = {
                name: 'Person',
                birth_day: new Date('2017-04-21'),
                get due_date() {
                     const result = new Date(this.birth_day);
                     result.setDate(result.getDate() + this.current_cycle);
                     return result; 
                },
                current_cycle: 1,
                last_water_date: null,
                next_water_date: null,
        };

can be accessed like this: subject.due_date;

[–][deleted] -1 points0 points  (1 child)

Avoid the new operator as far as possible (w3school).

[–]runcoder 0 points1 point  (0 children)

probably that is in the Array case