What's new() in node.js - javascript

I'm studying the WebRTC tutorial from Google's codelab.
In its index.js section for Node.js. I has the following line.
var fileServer = new(nodeStatic.Server)();
I understand new is an operator to create instances. But it above is using new(nodeStatic.Server)() which confuses me.
Can someone please explain it for me? Thanks.

nodeStatic.Server is apparently a constructor. So, your code works like this;
let serverConstructor = nodeStatic.Server;
let fileServer = new serverConstructor();
The parens are used in your original code to make the competing precedence between new, () and the . operator a little more obvious. You'd have to study the precedence rules in the Javascript spec to see if the parens around nodeStatic.Server are actually needed or not (it appears they are not, but it takes some detailed knowledge of the spec or running your own tests to know that).

Related

Can someone please confirm that gets() is not part of the JavaScript Standard Library?

I recently worked my way through a developer pre-screen via an online candidate testing platform. I've had bad experiences with these platforms in the past - poorly written/structured code, unclear instructions and code comments - but I'd yet to encounter a JavaScript question that (possibly) included C syntax ... until today.
The question was straightforward enough: create a function that took in a single parameter (milliseconds), converted that parameter to a new Date object and then returned only that Date's day-of-the-month.
Here was the boilerplate code I was asked to start with:
function convertMilliseconds(milliseconds) {
var result = -404;
return result;
}
var milliseconds = Number(gets());
console.log(convertMilliseconds(milliseconds));
Edit: To clarify, gets() was not explicitly referenced in the question's prompt, nor were there are any code comments in the web-based code editor asking me to implement it as a custom function. Here is a screen cap to illustrate:
Can someone please confirm for me that gets() is not part of the JavaScript Standard Library?
I've consulted the MDN docs and did not find anything in the entry for Number to indicate this was valid code. I did, however, find references to C library function called gets().
I try and make a point of using these pre-screens to fill any conspicuous gaps in my JavaScript knowledge. I'm pretty sure this is just a glaring error on the testing platform's part; I just want to be doubly sure I'm right before writing this off and moving onto other things.

Exclamation point and dot in JS or TS (ex: obj.prop!.value)

I was reading this article on using observables and came across several instances where the writer wrote something like this:
map((value: IObj) => {
return value.name!.value; // << What is this??
})
I've never seen this exclamation point and dot combo before. The writer does this a few times in the article for me to think that it's a typo. I know about ?. but I've never seen !. in JS, TypeScript, or Flow.
Can someone help me understand what that is? Specifically:
what is it called?
Is it JS or TS?
If JS, when was it introduced?
Most importantly, what does it do?

Advice on migrating a web app source code from Coffeescript to javascript

I've inherited responsibility for a moderately complex Rails app, which has meant learning Ruby and Rails besides trying to understand a lot of code that I did not write myself. The project also contains a non-trivial Backbone.js app written in Coffeescript. Since I will be the sole developer on this project for a long time, and since I don't know Coffeescript, I plan to move the entire source code to straight Javascript.
I would like to know what the best approach to doing this would be. Compiling to javascript is easy enough, but there is a lot of refactoring to do to make the code look "normal".
Cleaning up cruft by replacing stuff like
var a;
a = 1;
with
var a = 1;
is simple enough, or perhaps not even worth bothering with. I am more worried about the overall structure of the project. Coffeescript produces files that begin like this Backbone view code:
(function() {
var extend = function(child, parent) {
for (var key in parent) {
if (hasProp.call(parent, key)) child[key] = parent[key];
} function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype; return child;
},
hasProp = {}.hasOwnProperty;
App.Views.MyClass = (function(superClass) {
extend(MyClass, superClass);
function MyClass() {
return MyClass.__super__.constructor.apply(this, arguments);
}
# rest of the code here...
});
}).call(this);
I would just like to know what a sane approach to dealing with this would be, and I haven't found any kind of "best practices" for doing what I would like to do.
What would be best: just check everything into git and keep going? Use the Backbone/Underscore version of extend instead of redefining the same function at the top of every file? Use a different method entirely for stringing together all of the separate files?
Just looking for a general direction. I'll figure out the details.
You could try using a tool like decaffeinate if you are ok with using ES6.
Otherwise, if you are sure you want to port to Javascript, then you'd be better rewriting by hand, than trying to fix the generated code.
Start by recreating the files one at a time, writing unit tests as you go. Not only will this help you better understand the project, but you'll also learn Coffeescript (and probably some good techniques as you go).
If you read the overview on the Coffeescript website, it provides a translation table for going between the two languages. Every time you come across syntax you don't understand, see what it generates when you enter it into js2.coffee. There's another good reference for migrating from Coffeescript to ES6 here.
You will end up spending more time and much more effort (not to mention most likely introduce bugs trying to replicate things like super or sub-classes in CoffeeScript). Instead of trying to rewrite a working CoffeeScript web app, just dig in and soak up the CoffeeScript as you go along and find the need to change things. Stop resisting! CoffeeScript is well documented and has proven itself as a very useful language to know. Learn it, its only scary because its new!

Are commas necessary in Node.js?

Are there risks incurred when omitting commas in variable declarations for node.js? For example, declaring some global variables like the following works just fine:
express = require('express')
jade = require('jade')
And I don't want to write commas if it's safe not to write them (I don't care about "beauty/clarity of code" arguments).
Important: I mean commas, not semicolons (got 3 answers about semicolons). It's perfectly OK and even recommended to remove semicolons from node.js. The creator of npm also does it: http://blog.izs.me/post/3393190720/how-this-works
If in doubt, check the latest javascript specs: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
Note that you also don't need to write
var
for global variables.
But this question is about "commas" so please don't replace commas by semicolons by mistake when editing my question (done before).
In JavaScript, if you don't write semicolons ; they will be inserted for you, invisibly. And you may not always like where they go.
You are not technically required to end every statement with a semicolon. However, most consider it a good idea.
For more info, peruse through the results of this google search. We've been arguing about this topic for a long time.
Here's an example of why this is more complex than it appears at first glance. While you are not technically required to end every statement with a semicolon, there are a few cases where you MUST, or things break. You cannot completely omit them in most codebases.
foo.def = bar
(function() {
// some self executing closure
})()
Looks simple enough right? Well the interpreter looks at that and does this:
foo.def = bar(function() {
// some self executing closure
})()
Which is probably not what you were expecting. The way to fix it, is with a semicolon.
foo.def = bar;
(function() {
// some self executing closure
})()
There a lot of cases like this. You can either learn them all, and only use them in those cases, and when you inevitably forget you try to debug your code that's doing something so strange and bizarre that you tear your hair out hours... "what do you mean wtfvar is not a function?!? It's not supposed to be a function!"
Or you can just use semicolons with consistency.
In short, no. The problems you are likely to run into will arise when you go to do things like code minifying and the compiler thinks that your two statements are one. Anyhow, if you choose not to use commas/semicolons, which is absolutely not recommended, you should be fine.
Node.js uses the V8 engine to read your code so it will pretty much behave like in Google Chrome. That said, not using semicolons is generaly a bad practice. The interpreter will try to understand your code and may be sometime mistaken (by your fault).
Check this out for a full explanation: Do you recommend using semicolons after every statement in JavaScript?
This very late answer just goes to clear confustion for others that might read this.
Since you're actually talking about commas, not semi-colons, I can only assume that you have a misunderstanding of what is implicitly being added by the engine.
Commas are not optional. And this code:
express = require('express')
jade = require('jade')
is being implicitly converted into this:
var express = require('express');
var jade = require('jade');
not this, which you might be expecting:
var express = require('express'),
jade = require('jade');

Does "go to definition" work in Eclipse for javascript?

Working with Eclipse for Javascript, Ctrl-click seems to work on some objects but will not take me outside of the current javascript file. Is there any way to get this "go to definition" to work more fully? I use Eclipse for Java and depend on this functionality, would like to see it work better in Javascript as I'm just trying to learn Javascript.
I think it probably doesn't work because of the many ways in JavaScript to define something..
function foo() {}
var foo = function() {};
window.foo = function() {};
window['foo'] = function() {};
var z = 'foobar'; window[z.substr(0, 3)] = function() {};
Especially the last one would be - even though it's unlikely to be ever used in real code - pretty much impossible to be detected by an IDE without executing the whole code and then tracking where a global is defined for the first time.
Another example would be with libraries implementing a class system. Without knowing the details of every library it's pretty hard to find out what class names they define.
Intellij Idea support that functionality. I was looking to see if Eclipse has a plugin and came across your post, I use to work with Intellij Idea and I have that functionality that is very helpful so to the user that is saying that is impossible for a IDEA please take a look in Intellij Idea you will be surprise of all the functionality that you can find.

Categories

Resources