$ php -r '::'
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
Wat $ php -r '::'
Parse error: parse error in Command line code on line 1
How'd you get the weird error?Edit:
Hrm, if I try it on my DreamHost account, I get something more similar to yours:
$ php -r '::'
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in Command line code on line 1----- jsc ------
[] + []
> [] + {}
[object Object]
> {} + []
0
> {} + {}
NaN
------ node.js ------ > [] + []
''
> [] + {}
'[object Object]'
> {} + []
'[object Object]'
> {} + {}
'[object Object][object Object]' d8> ([] + [])
d8> ([] + {})
[object Object]
d8> ({} + [])
[object Object]
d8> ({} + {})
[object Object][object Object]
So I believe this is merely erroneous behavior of the console, not a weirdness of JavaScript itself.I didn't mention any of this in the talk (it would've killed the flow ;). Instead, I glossed over it and interpreted the syntax as any sane programmer would.
Also, I'm on an old version of node, but my output matches jsc.
I think the actual video was a little misleading, [] + {} == the string "[object Object]" not an object. The square brackets are just part of the tostring method and are unrelated to the square brackets of arrays.
But maybe it is just because I write code for a living...
(![]+[])[+!+[]]
produces an "a".JavaScript went down the path of being a "forgiving" language because it was intended to provide "extra" (non-core) functionality, under a lot of environments, in the hands of non-experts. It made sense at the time that it should fail silently rather than noisily to not crash the page it was on. It also tried to help amateurs who just banged on code until it did what they wanted, by generally doing something rather than nothing, and by trying to infer intended behavior from undisciplined code. It was never intended to make large-scale or robust applications, so it didn't make decisions that would facilitate that use-case.
Maybe someday...
a = a
in your code, Ruby creates the variable a before it ever tries to actually execute the assignment. If later, when it has finished parsing everything, and starts executing, that statement fails because b is not defined, you still end up with variable a being defined, and since it has not had anything successfully assigned to it, it has a value of nil.The JavaScript stuff, I think, comes from operator overloading. The plus operator is overloaded to allow adding strings to concatenate them, and it will do type conversion to get compatible types, so "wat"+1 results in the 1 being converted to a string, and then the strings are concatenated. Since the minus operator is not so overloaded, "wat"-1 instead is treated as numerical subtraction. JavaScript allows string to be used as numbers, so "123"-1 gives 122. However "wat" is not a string that represents a number, so gives NaN when forced to be treated as a number, and "wat"-1 is thus NaN.
Fun fact: the creator of Javascript posts on HN. Maybe he'll answer your question.
I managed about halfway through. I did have the audio turned off, so I'm assuming I missed out on some of the redeeming qualities it may have had. Given the liberal usage of "wat" throughout what I did watch, I suspect that my gut feel is likely correct.
Anyway, guess I'm not a hacker either.
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
You can just do:
sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /bin/jsc
To be able to invoke it directly from the command line.
I suppose if you are a WebKit-only developer, this utility could have many useful applications.
Perhaps some memes should never be vocalized.
That's 15 commas, not 16.
function pad(n, c) { return Array(n+1).join(c); }
// pad(6, "-") === "------" Array(16).join("wat" - 1) + " Batman!"
?Empty strings separated with NaNs look perfectly logical to me (I'm a JS guy though, so, well, bear with me).
The first thing it should do is TypeError that null is not a string. Or don't join null fields because there is nothing there.
If is does convert null to '', then it should TypeError on string - int. If it is not going to TypeError it should do something sensible, like slice the string
> Array(16).join("wat".slice(0,-1)) + " Batman!"
'wawawawawawawawawawawawawawawa Batman!'
I think the correct thing would be to TypeError in two different places. Or change the language so things that don't error actually work. Returning a string with NaNs in it will just lead to uncaught errors. If it actually returns something it should probably be ' Batman!'.JavaScript nuttiness:
Now it's zero:
> null + 1
1
Now it's "null": > null + "text"
'nulltext'
Now it's a blank string: > [null, null, null].join()
',,'
Python errors when things don't make sense, and forces you to make them make sense:If it doesn't work, error:
>>> "wat" - 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'int'
Can't join None into a string: >>> ','.join([None, None, None])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, NoneType found
Convert None to a string if you want: >>> ','.join([str(i) for i in [None, None, None]])
'None,None,None'
But it makes more sense to drop the None: >>> ','.join([str(i) for i in [None, None, None] if i])
''EDIT ok my bad it was mentioned on the screencasts homepage