1

I know how to call a method from an object i.e. the following if myObj.objMethod is called it will return string.

myObj = {
  objProperty: 'string',
  objMethod: function() {
    return this.objProperty
  }
}

However, I am attempting the following code wars exercise and cannot figure out what needs to be done. It looks like they want the function to be called within itself. To do this i have tried using arguments.callee.call(MyObject.objMethod()) but, as expected this exceeds the max call stack. Has anyone got any idea if this is possible to call a method of an object of a function, from within that function?

Here is (one of) my attempt(s) below:

function myFunction() {

  var MyObject = {
    objProperty: "string",
    objMethod: function() {
      return this.objProperty;
    }
  }

  return arguments.callee.call(MyObject.objMethod());

};

Here are the code wars instructions:

Property objMethod should be called by myFunction.

Can you fix the syntax so myFunction will be working again? Please check things like braces, commas, and letter case.

and here is the original code provided:

function myFunction() {
  var MyObject = {
    objProperty: "string",
    objMethod: function() {
      return myObject.objProperty;
        }
  }

  return myObject.Objmethod();
};
3
  • return MyObject.objMethod(); Commented Jan 9, 2017 at 12:05
  • @MirkoVukušić i tried that firstly, but didn't work Commented Jan 9, 2017 at 12:05
  • In your linked example you have cases wrong. You wrote return myObject.Objmethod(); ... note wrong casing Commented Jan 9, 2017 at 12:07

2 Answers 2

2

This is your solution

function myFunction() {
  var MyObject = {
    objProperty: "string",
    objMethod: function() {
      return MyObject.objProperty;
    }
  }

  return MyObject;
};

The problem statement is

Property objMethod should be called by myFunction.

It passes all the tests.

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

4 Comments

yeah, that was it! ok, guess i misunderstood what they were looking for a bit
This is correct answer to pass the test. However, please note that your initial problem was a lot of typos in your code. You would probably come to this conclusion yourself if you were able to run your own code. In example: 1) you were missisng a comma after objProperty: "string", 2) you called myObject.Objmethod(); and object was MyObject and method was objMethod, etc.
@MirkoVukušić That was the actual problem statement. One had to fix the errors in it.
@ClydeLobo I'm not questioning correct/best answer on this. It's definitely yours. I just wanted to make sure he understands where he was wrong. Because if he just copied your code into the test he might think only issue was to return MyObject instead of MyObject.objMethod and he would have missed all the other stuff.
0

Make sure you look at the test and what it is expecting. The test is expecting to be able to reference the object, not one particular property.

return MyObject;

Comments

Your Answer

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