admin管理员组

文章数量:1328836

I'm not talking about in the URL. I know what that does. I'm talking about how it's used in actual code.

After trying to assign it as a variable, I realized that it's reserved, but I don't know what for.

I'm not talking about in the URL. I know what that does. I'm talking about how it's used in actual code.

After trying to assign it as a variable, I realized that it's reserved, but I don't know what for.

Share Improve this question edited Jun 1, 2024 at 13:17 dumbass 27.3k4 gold badges37 silver badges73 bronze badges asked Mar 8, 2011 at 15:38 McKaylaMcKayla 6,9595 gold badges39 silver badges49 bronze badges 2
  • So you're trying to do var # = "foo" and # is reserved? – Surreal Dreams Commented Mar 8, 2011 at 15:43
  • 1 Could you specify what you mean? – Johannes Fahrenkrug Commented Mar 8, 2011 at 15:50
Add a ment  | 

5 Answers 5

Reset to default 2

An ongoing proposal (currently in stage 4) utilizes hashtags to mark fields as private. It is part of the ES2022 standard.

Example:

class Foo {
  x = 1; // public field
  #y = 2; // private field

  add() {
    return this.x + this.#y;
  }
}

Javascript, or more precisely ECMAscript, is an evolving language. Some symbols and keywords (such as "class") have been reserved for future versions, even though they may not have any meaning at the moment.

I dont think, that this sign is somehow reserved fot another functionality. I found that rule here:

You must not use any punctuation marks of any kind in a JavaScript variable name, other than the underscore; for example... some:thing or big# or do'to would all be illegal.

This ist just, that javascript does not accept punctation signs in variable names ant due to this not parsing variables named like this as variables.

See here: What characters are valid for JavaScript variable names?

In JavaScript variable names, no punctuation marks are permitted except for the underscore (_) and dollar sign ($). You can't start a variable name with a number, but otherwise all letters and numbers are permitted in a variable name.

So you can't have a variable name with # in it, no less a variable named #. It has no special meaning, it's just not permitted just as variable can't be named ~.

本文标签: What is the hash character () used for in JavaScriptStack Overflow