0
function makeGroup(param) {
  this.group = param;
}

duplicateEmployee.makeGroup(index2);

Doing a simple duplicateEmployee.group = index2 doesn't work. Trying a method says "makeGroup is not a function."

Here's the full code for reference.

var someVar = [];
vm.contacts = ContactsService.query(employees => {
  employees.forEach(employee => {
    employee.groups.forEach((groupMembership, index2) => {
      // getEmployee() works...
      function getEmployee(param) {
        return param;
      }

      // "makeGroup is not a function"
      function makeGroup(param) {
        this.group = param;
      }

      var duplicateEmployee = getEmployee(employee);
      if (groupMembership) {
        duplicateEmployee.makeGroup(index2); // this breaks it. Says makeGroup is not a function
        console.log(groupMembership, index2); // index2 is working... that's the group number
        someVar.push(duplicateEmployee);
      }
    });
  });
});
vm.contacts = someVar;

Tried this and it doesn't work

      function makeGroup(param1, param2) { // console.log(param2) === undefined
        console.log(param1); // group number... not the employee (still useful)
        console.log(this); // 'this' is the employee, so lets try this.group
        this.group = param1; // still overwriting the variable
      }

      makeGroup.call(duplicateEmployee, index2)
7
  • 2
    duplicateEmployee is just a variable that has whichever value param has, it doesn't have a function called makeGroup Commented May 30, 2017 at 22:28
  • That is because your employee object has no makeGroup function. Commented May 30, 2017 at 22:28
  • so instead of function makeGroup it should be function duplicateEmployee.makeGroup (param) {}? Commented May 30, 2017 at 22:29
  • @TravisJ that's why I'm confused with methods. Normally functions just work, and I'm not sure the difference. Commented May 30, 2017 at 22:30
  • No, for that to work you'd have to make an Employee kind of class, add makeGroup to it's prototype, create a new class in your code, and then call that function. Also I don't get the point of creating those two functions only available in your forEach scope. Commented May 30, 2017 at 22:31

2 Answers 2

2

You're calling a method makeGroup of a duplicateEmployee object. This object does not have this method in it. You can either add it to its prototype or just change makeGroup so that it's a function that takes in the employee and the param, and it'd be something like so.

function makeGroup(employee, param) {
  employee.group = param;
}

The reason getEmployee works is because you're calling it from the global context, and not from a duplicateEmployee context like you're doing with makeGroup.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. employee.group keeps getting overwritten when I try this. It would be the same as saying duplicateEmployee.group = index2
That's because your duplicateEmployee isn't actually getting a copy of the employee you want, it's getting a reference to it. So whenever you change duplicateEmployee you're also changing employee. Can you tell me if the problem is fixed if you do return Object.assign({}, param) instead of return param in getEmployee?
1

duplicateEmployee.makeGroup(index2); can't be called from the employee because it's not a member of that.
You can just call it like that makeGroup(index2)

6 Comments

I have my doubts if this is going to work once inside makeGroup it is using "this". Maybe he could pass an employee as a parameter or invoke through makeGroup.call(duplicateEmployee, index2)
So should I create a whole new method to construct the employee for this one? Something like this w3schools.com/js/tryit.asp?filename=tryjs_create_object3
@dime2lo "CreateListFromArrayLike called on non-object" when I try makeGroup.apply(duplicateEmployee, index2) and function makeGroup(param1, param2) { param1.group = param; }
@TylerL I edited the response (sorry). It must be called with call not apply, using apply have to pass parameters as array. It should be makeGroup.call(duplicateEmployee, index2). Regards.
I think we're getting there. I'll modify the original question with your suggested update.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.