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();
};
2 Answers 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.
4 Comments
objProperty: "string", 2) you called myObject.Objmethod(); and object was MyObject and method was objMethod, etc.MyObject instead of MyObject.objMethod and he would have missed all the other stuff.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;
return MyObject.objMethod();return myObject.Objmethod();... note wrong casing