0

I have a function of the prototype of an object called getComponent which returns this.components[name]

function Foo()
{
    this.components = {"Scripts": [...], "Sprites": [...], "States": {...}, etc };
}

Foo.prototype.getComponent = function(name)
{
    return this.components[name];
}

Then I have some code that gets a component and sets it to something else...

var foo = new Foo();
foo.getComponent("Sprites") = [];

This obviously raises the Uncaught ReferenceError: Invalid left-hand side in assignment.

Is there anyway to set the value of the returned value?

I don't want to access the objects variables directly: foo.components.Sprites = [].

Thanks in advance,

David

1
  • 1
    you could create a setter method just like the getComponent method to update the value Commented Jun 5, 2017 at 12:55

1 Answer 1

2

No.

You can assign values to variables, constants and to the properties of objects.

A function returns a value. It never returns a variable, constant or property (not even if it reads one of those to get the value).


Either:

  • create a setComponent method which takes two arguments and assigns the value of the second one to the property.
  • rename your method to component and have it take an optional second argument and then act as either setComponent or getComponent depending on how many arguments were passed.
Sign up to request clarification or add additional context in comments.

Comments

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.