0

Let's say I have some class with some function inside:

var someClass = class someClass() {
 constructor() {}
 someFunction(someObj)
 {
 function anotherFunction() {
 return JSON.stringify(someObj, null, 2);
 }
 return someObj;
 }
}

In this context, I could call someClass.someFunction(someObj), and it would return [object Object]. Upon trying to call someClass.someFunction(someObj).anotherFunction(), it comes up with a TypeError: someClass.someFunction(...).anotherFunction is not a function.

How could I circumvent this? I tried creating a prototype like someClass.prototype.someFunction.anotherFunction = function() {...}, but that doesn't work.

Many Thanks in advance, @Medallyon.

asked Sep 11, 2016 at 15:58
1
  • Wouldn't you just need to return {anotherFunction}; instead of return someObj;? Commented Sep 11, 2016 at 16:01

2 Answers 2

1

You're creating a function (anotherFunction) and never doing anything with it. The JavaScript engine probably optimizes it away entirely.

If you want it to be accessible outside someFunction, you need to do something to make it accessible outside someFunction. You might return it. Or return an object that has it as a property. Or add it as a property to someObj (though that would be unusual).

So for instance, here we return a new object with both the function and the original someObj on it (because you were returning that for some reason):

var someClass = class someClass {
 constructor() {}
 someFunction(someObj) {
 function anotherFunction() {
 return JSON.stringify(someObj, null, 2);
 }
 return {
 someObj, anotherFunction
 };
 }
};
var sc = new someClass();
var o = sc.someFunction({
 foo: "bar"
});
console.log(o.anotherFunction());


Side notes:

  • Your someClass declaration is incorrect; you shouldn't have () after in var someClass = class someClass() {.

  • The var someClass = part of that is pointless if the variable name and class name are the same. If they're different, at least use let so the hoisting is the same for the variable as it would be for the class (e.g., it's only half-hoisted).

  • The overwhelming convention in JavaScript is for constructor functions to start with a capital letter; so SomeClass, not someClass. While you can use any convention in your own code you like, I would strongly recommend using this one, it helps people reading your code. At the very least, follow it when asking questions.

answered Sep 11, 2016 at 16:02
Sign up to request clarification or add additional context in comments.

2 Comments

How would I access someObj from o in this scenario? I tried doing o.someObj, but that seems to produce a very weird object: i.imgur.com/DTyKz3e.png
Never mind, I found out that the someObj object that I passed to anotherFunction() is like that already.
1

someClass.someFunction(...).anotherFunction does not refer to an "inner" function of someFunction; instead, it refers to the function anotherFunction on the object that is returned by someFunction. Thus you need to return some object that has anotherFunction as a member function for this to work.

Example:

var someClass = class someClass() {
 constructor() {}
 someFunction(someObj)
 {
 function anotherFunction() {
 return JSON.stringify(someObj, null, 2);
 }
 // note the difference here
 return {
 // anotherFunction is a member function of the return object
 anotherFunction: anotherFunction,
 };
 }
}
answered Sep 11, 2016 at 16:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.