0

Boredom got the best of me last month, and I've been working on a library for JavaScript ever since. I've read about 20-something tutorials, and plenty of documentation.

And now that I have a tiny, simple (but working) library, I want to change the syntax that is used when writing the code.

And I think I may have just found a way to do it. Or, more possibly, I may have just embarrassed myself with the following line, and question:

string     = new String('string');

That line up there; does it create a new type of type string, and call it string? Or does that line do something else, or is it basically the same as:

string string = "hey";

The reason I am asking is, I am interested in learning how to change things. I would like to be able to declare a new string like this, in my tiny js library:

txt someString = "hey";

... where txt is the data type 'string'.

And then use it like this:

if(someString == "hey") { return true; }
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
Arrow
  • 2,784
  • 8
  • 38
  • 61
  • 2
    Neither `string string = "hey";` nor `txt someString = "hey";` can possibly be JavaScript syntax. There's only one type of string primitive in JavaScript, and you can't make more. – Pointy Oct 20 '12 at 21:39
  • 2
    Also here's a tip: stay away from w3schools, and in general don't trust much JavaScript information from before about 2010. – Pointy Oct 20 '12 at 21:41
  • Yeah, no offense to anyone, but I've never trusted W3Schools all that much. – Arrow Oct 20 '12 at 21:46
  • What you're trying to do seems (in my opinion) to be kinda pointless, there are various ways it 'could' be done (see @Bergi's answer) but first ask 'why' you would want to. Instead look at how jQuery, Dojo, Require, etc handle these types of things. There's no point reinventing the wheel (unless you have a *really* good reason to do so) – Bob Davies Oct 20 '12 at 23:46

2 Answers2

2

string = new String('string'); creates a new object (being an instanceof String) which has the internal value "string" (a string [primitive value]) and assigns it to a variable whose identifier is named "string". The second and third lines are invalid in JavaScript.

> var strObj = new String("string");
> var strVal = "string";
> typeof strObj
"object"
> typeof strVal
"string"
> strObj == strVal
true
> strObj === strVal
false
> strObj instanceof String
true
> strVal instanceof String
false

Usually, you never ever need String instances. If you access methods of string values in JS, they are automatically coerced to objects.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thank you for the explanation. Is it possible to change the way we declare a String in a js library without changing the underlying data type? Or am I using my imagination too much? – Arrow Oct 20 '12 at 21:42
  • You don't *declare* strings in JavaScript, which is a loose-typed language. What exactly do you mean by "*underlying data type*"? Strings are strings, there is only one type for textual data. Those objects are only needed for String methods by prototypical inheritance. – Bergi Oct 20 '12 at 21:47
  • What I mean is, instead of writing `string msg = "hi";` I want to `write txt msg = "hi";` – Arrow Oct 20 '12 at 22:01
  • What is `txt`? There is only one string type in JS, and variables (like `msg`) have no type. – Bergi Oct 20 '12 at 22:04
1
txt someString = "hey";

You can not change Javascript's syntax. If you really want to do that, you'll have to write your own programming language. See Learning to write a compiler. If you want this programming language to be usable in a web browser, you will need to compile your language to Javascript. CoffeeScript is one such language that does exactly this.

Community
  • 1
  • 1
Olivier Lalonde
  • 19,423
  • 28
  • 76
  • 91