3 ms·
Re: "The $ before all variables is a feature [...] but we think it adds readability." The argument for the $ sign is not readability. If it were you'd see Java
by KrisJordan 16y ago
Re: "The $ before all variables is a feature [...] but we think it adds readability."
The argument for the $ sign is not readability. If it were you'd see JavaScripter's prefixing all of their variables with a dollar sign, too. var $foo = 'bar'; is completely valid JavaScript. (Aside: $ prefix is handy in JS when holding references to jQuery objects ie $container = $('#container');)
The argument is for simplified string manipulation with magic quotes and is an homage to unix shell scripting.
Without the $ prefix string manipulation would be reserved to concatenation/printf/replacement/etc. With the $prefix you can do things like:
$base = __DIR__;
$user = (object)array('name'=>'Foo','dir'=>"foo");
$size = "lg";
$image = "$base/users/$user->dir/images/$size.jpg";
vs:
image = base + "/users/" + user.dir + "/images/" + size + ".jpg"
When doing a lot of string manipulation and scripting the $ is quite handy.
The dollar sign also enables variable variables and dynamic property access. i.e.
$foo = 'Hello, world.';
$bar = 'foo';
echo ${$bar}; // 'Hello, world.'
- and -
$user = (object)array('name'=>'Foo','dir'=>"foo");
$prop = 'name';
echo $user->{$prop}; // 'Foo'
The dollar sign for variable variables is weak because variable variables is usually an indication of code gone awry. Dynamic property access, on the other hand, is much more useful in a dynamic language like PHP/JavaScript. That said, I believe JavaScript's solution is much more elegant. i.e.:
var user = {name: "Foo", dir: "foo"};
var prop = "name";
alert(user[prop]); // 'Foo'
By overloading the index operator on objects to accept a string that dereferences to an object's property you get the same benefit in a much more elegant way.
Thus the $'s primary justification is magic strings and string manipulation.
- wvenable 16y agoThe $ also completely separates variables from the other namespaces, like reserved words. You can have variables named $if or $while and not worry about conflicts now or in the future.
- WilliamLP 16y agoYes, all that, plus readability. I know when something is a variable. Prefixing variables in JS with $ doesn't do this because I still have to look out for things without a $ that are variables as well so it's the worst of all worlds. > (Aside: $ prefix is handy in JS when holding references to jQuery objects ie $container = $('#container');) I really really don't want to see that in a project I'm in charge of. It looks terrible to me, and I really don't like $ as an ordinary alphanumeric character. I like $ in jQuery projects to only be used for jQuery calls. (If it doesn't have to co-exist with something else and use noConflict mode.) > var user = {name: "Foo", dir: "foo"}; I hate that about Javascript, how in object literals the left side is a literal string and the right side is an expression. Except, I really should quote the left side anyway - I think Crockford recommends this - because I might want a hyphen in there, or I might want to use a keyword (like "interface" where it isn't even obvious that it actually is a keyword.) Also, if I have var key, and var value, I want to be able to pass it into a function that takes a dictionary argument as {key: value}, and not have to do var dict={}; dict[key]=value; I also think Javascript absolutely begs for a real dictionary type. Objects don't work so well because I have to worry about someone extending them and I have to use hasOwnProperty - never mind someone could override hasOwnProperty. Also the performance characteristics of JS objects are terrible - Did you know that in Firefox a switch statement is one hundred times faster than an object lookup? PHP's dual purpose variables are ugly as hell (with object -> syntax and array [] syntax) but I actually like being clear on whether I'm using it as an object or a simple array/dictionary.
- wingo 16y ago> Did you know that in Firefox a switch statement is one hundred times faster than an object lookup? A switch statement in what language? I presume by "object lookup" you mean the "bar" in "foo.bar" in javascript. With all respect, I think you are worrying too much :)
- WilliamLP 16y ago> A switch statement in what language? Javascript. > I presume by "object lookup" you mean the "bar" in "foo.bar" in javascript. Yes, but more specifically what I tested was the foo[bar] form, where bar contains a string, versus switch(bar). > With all respect, I think you are worrying too much :) For a language that is supposed to be nearly ready for writing rich interactive applications including pixel-by-pixel manipulation, two orders of magnitude (and widely variable across browsers) is a huge deal. I'm guessing you haven't tried to do much canvas programming?