0

Consider:

function a() { a = null; }

Why can't a be assigned?

9
  • Because the function has to be created before it can be destroyed, and once it's destroyed, it's no longer created, so it cannot execute anymore. To put it another way, a snake can't eat it's own tail. Commented Jul 28, 2014 at 18:16
  • 3
    What would be a practical use case for this? Are you trying to write a DestroyUniverse() function? Commented Jul 28, 2014 at 18:18
  • 2
    Ah, see there's probably a much better way to do that. Ask us how to do that (be specific), instead of trying to come up with your own solution and asking why it doesn't work. Commented Jul 28, 2014 at 18:21
  • 1
    @RobertHarvey, Can't see why a question I asked I can't be addressed regardlessly of my way of doing things. Commented Jul 28, 2014 at 18:28
  • 2
    @RobertHarvey, that's unrelated. I wasn't looking for a solution in the first place, because I didn't have a problem. I just made an observation about TypeScript by accident and my curiosity was the only reason why I came here. If asking questions unbound to specific problems is a crime, then I beg your pardon. Commented Jul 28, 2014 at 18:45

2 Answers 2

4

The spec seems quite restrictive here, but then function definitions behave quite strangely, so it is understandable. (That is, you can call them earlier in the source code than they are declared... strange!)

Only reference expressions can be assigned to, which are defined in Section 4 of the spec as:

... references are combinations of identifiers (section 4.3), parentheses (section 4.7), and property accesses (section 4.10).

and in 4.3,

An identifier expression that references a variable or parameter is classified as a reference. An identifier expression that references any other kind of entity is classified as a value (and therefore cannot be the target of an assignment).

So, you could change it from a function definition into a variable declaration, probably with little change to the rest of your code:

var foo = function() {
    foo = null;
}
Sign up to request clarification or add additional context in comments.

1 Comment

There you go 10k. Congrats.. :)
1

TypeScript disallows assignment to declared functions because, as you've seen in the comments, it's a very rare thing to do intentionally. It's much more likely that you had a typo (you meant to assign to aa instead of a) than that you actually wanted to overwrite a declared function.

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.