Javascript regex shorthand? - javascript

I'm trying to enjoy some of the awesome javascript code golf submissions on anarchy code golf, but I keep seeing things like:
for(;s=readline();)print("h"+/t.*/(s))
...which was the JS winner for: http://golf.shinh.org/p.rb?ttp
I don't understand how that is correct javascript syntax, and I even tried resubmitting that, but it said object is not a function, which is something along the lines of what I would expect to happen.
Was this some kind of glitch or shorthand or something in an older javascript version?

Was this some kind of glitch or shorthand or something in an older javascript version?
More or less, yes. According to that site's version info, it uses SpiderMonkey (Mozilla's JavaScript engine), which used to have the feature that regular-expression objects were callable; that is, that if re was a regular-expression object, then re(...) was equivalent to re.exec(...). That feature was removed in this change, a result of Bug 582717, and that site has since updated to a version that incorporates that removal.

Related

VS Code intellisense for Javascript built-in function

In Javascript for some methods, I don't see the suggestions. For example indexOf, charCodeAt and so on. Is it possible to activate suggestions for these built-in Javascript keywords?
I think you should leverage VS Code ability to work with JSDoc.
As you can see without JSDoc, VS Code can't infer that bar is a string. After documenting the parameter properly, it is able to make some meaningful suggestions:

dart's latest js-interop library causes some issues lately

I have seen some strange changes in the latest js-interop library and I wonder if anybody can give some clarification about it.
After upgrading to the latest version I noticed dart2js doesn't work anymore. After some investigation I concluded the cause was an #proxy annotation inside the js-interop library ( also mentioned here).
Because of this I switched between a couple of versions and I noticed some functions like scoped have been deprecated in a timespan of only a couple of days after which it has been completely removed! If you missed the in between version in which this function is marked as deprecated you miss the hint which give's some info about it. Also note that the main tutorials about the js library on dartlang.org don't even give a hint about the fact that large parts of it have become outdated.
I decided to go back to version 0.0.26 and although it seemed to work as before... I noticed in one instance when retrieving a variable from the javascript context in dart I received a dart DateTime object and not a js.Proxy object.
Going back a version earlier (v0.0.25) I got back my js.Proxy when using a variable from the js context, as expected.
The weird thing is that on github the versions go until 0.0.25 (which work as expected) and the one on pub has 3 versions more, which all break backwards compatibility a lot (which sometimes is needed) without clear instructions about what is going on (which I find lightly frustrating).
Can somebody give some clarifications about what is going on and what I can expect for the coming times?
http://pub.dartlang.org/packages/js
All this breaking changes have been announced in Future breaking changes in package:js and BREAKING CHANGE: package:js deprecated features removed.
Basically, starting from 0.0.26, package:js is now baked with dart:js. This change comes with several things :
scopes/retain/release are no longer needed
several types are now transfered directly between Dart and Js and not proxied
null, bool, num, String, DateTime
Blob
KeyRange
ImageData
TypedData, including its subclasses like Int32List, but not ByteBuffer
Node
Element from shadow dom can now be transfered as well
Callback are now longer needed.
a really big performance improvement
Here's a quick migration guide :
scopes : remove js.retain, js.release and js.scoped.
replace new Callback.xxxx(f) with f and remove callback.dispose().
replace Proxy with transferable type based on the above list.

Big super happy javascript compression

Now, I've heard of javascript compressors, used a bunch and favour a few. However, they all do the same thing. Remove unnecessary space. That's great, they do exactly what it says on the tin. Compresses Javascript. However, looking through some of the major players that provide legendary libraries (such as jQuery), they offer "minified" sources that are entirely unreadable. Notably, the variable names change from someThingLikeThis to c. This is compression that I cannot seem to find anywhere.
My question is, where can I find a Javascript compressor which compresses variables in addition to removing unnecessary space. Or is it done manually?
For example:
// My Javascript:;
var cats = 'Nyan',
dogs = 'Hound';
alert(cats + dogs);
// jQuery styled compression:
var a='Nyan',b='Hound';alert(a+b);
As far as i know http://developer.yahoo.com/yui/compressor/ Does what you need :)
That should be done by a standard minifier. If it is not, then most likely the variable names just cannot be renamed safely (global variables/functions).
Also what you might be looking for is obfuscator. Check this question:
How can I obfuscate (protect) JavaScript?
Google Closure Compiler is the most advanced tool to transpile/minify JavaScript code.
It basically features two compilation levels—simple and advanced. You can use the simple compilation level on pretty much any JS code.
The true magic is in the advanced level which removes unused code, inlines functions, flattens properties (abc.def.ghi -> a) and renames all custom variables. But you have to write the code in a way the compiler can understand.
If you're serious about JS, read the "Closure: The Definitive Guide" by Michael Bolin who is one of the lead developers of the Closure Tools.

Is there any documentation on underscores in jquery variable names in IE8?

I was having issues with some jquery and posted about it here. After following a few of the suggestions, I was able to isolate the problem - IE8 did not like the variable name new_email. In fact the debugger had been telling me that the issue was at character 4 of that line, but I couldn't believe it was the variable name, so I kept looking for other issues.
After finally giving in and changing the variable name to newEmail, IE8 no longer blows up - the code works as expected with no errors.
I've been unable to find any documentation stating that you can't use underscores in jquery variable names, and indeed, the code worked correctly in every other browser with the underscore in place. Is this an unwritten rule in IE8? Is it something that real jquery developers just know? I'm worried if this really is true, as I inherited this code, and the app is enormous - I know there are several dozen variables in various places that have underscores in them.
This is actually a javascript variable and not a jQuery variable, an important distinction, and in Javascript the underscore is a valid character for variable names. You must have changed something else unrelated.
Is it possible that variable name was already assigned elsewhere? Also note that you aren't using the var keyword which can cause further issues with scope.
You can always post a jsfiddle.net example if you would like more assistance though.
here is a working jsfiddle that uses your variable
please note that you should probably be more specific than ":text"
jQuery is written in JavaScript, which is a language based on the ECMAScript Language Specification (PDF). The specification states that "underscore[s] are permitted anywhere in [a variable name]".
Your problem, as HurnsMobile, stated is most definitely not with the underscore but some other part of your code. It may also be caused by some quirk or bug in IE8 but even IE8 should be able to handle simple variable names.

JavaScript code completition done right?

I've tried some of the editors/IDEs regularly recommended for coding JavaScript (Aptana, WebStorm, ...) but none of them has a satisfying autocomplete functionality. I'm probably spoiled by Microsoft's IntelliSense for .NET. There is some JavaScript-IntelliSense in WebDeveloper, but that seems to be a stripped-down version. The best I've found so far is WebStorm, but its code completition is easily distracted by imported libraries (offering hundreds of suggestions) and identical function names.
Did I miss an editor/IDE that uses refactoring (or something else) to offer proper code completition, so that it really "knowns" what that variable-name stands for, I just put a dot behind? Or is something like this on its way?
I always recommend Komodo Edit from ActiveState (now up to version 6, with support for HTML 5 and CSS3 as well as recent versions of Javascript, PHP, etc.) Note that you may have to install addons for the languages you're working in, but you should find them through the Mozilla-like Addon manager.
Also supports jQuery and even lets you use jQuery (along with vanilla Javascript or Python) in its powerful macro IDE.
Code completion example:
<script type="application/x-javascript">
var obj = {};
obj.personnel = [{firstName:"John", lastName:"Brick", age:43},
{firstName:"Jane", lastName:"Motte", age:26}
];
// now type obj. and code completion immediately offers you "personnel"
// note: file must be saved for the app to find all members of declared
// variables, but I save about every 10 seconds so it's not a problem
</script>
The best I've found so far is
WebStorm, but its code completition is
easily distracted by imported
libraries (offering hundreds of
suggestions) and identical function
names.
This comment confuses me. If you import the libraries, and your code is using them, why is it bad to include the function names in the code completion suggestions? Wouldn't you want to have jQuery's functions included if you're using it?
If you're using Microsoft's IntelliSense with jQuery, does it stick to its guns and only show JavaScript core functions? Sounds limited to me, unable to be smart when I add libraries.
Or is something like this on it's [sic] way?
It sounds to me like you want a clairvoyant interface. I don't think it is on the way anytime soon.
By the way, "it's" == "it is"; "its" is the possessive.

Categories

Resources