Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Call method of object within function

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();
};

Answer*

Draft saved
Draft discarded
Cancel
4
  • yeah, that was it! ok, guess i misunderstood what they were looking for a bit Commented Jan 9, 2017 at 12:09
  • 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. Commented Jan 9, 2017 at 12:17
  • @MirkoVukušić That was the actual problem statement. One had to fix the errors in it. Commented Jan 10, 2017 at 10:52
  • @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. Commented Jan 11, 2017 at 0:28

lang-js

AltStyle によって変換されたページ (->オリジナル) /