Hot answers tagged

54

A benefit is that the compiler can let you know if you accidentally type "ADRESS" or "FEILDSET", and letting you fix it immediately instead of behaving in a nonsensical way at runtime. While the benefit is much more useful in statically typed languages than dynamic, it is still useful even if it is a runtime error as you will get message indicating a ...


36

Specifically speaking of JavaScript, you could use TypeScript instead. It offers some of the things you are referring to. Quoting the website: Types enable JavaScript developers to use highly-productive development tools and practices like static checking and code refactoring when developing JavaScript applications. And it is just a superset of JS, ...


19

Enums are useful for situations where you have a fixed set of values/entities that are sensible. They are self-documenting and allow the compiler validate things that would otherwise be left to run-time. They should never be used if the set of meaningful values is not known or not strictly limited. A more useful example would be something like HTTP ...


19

There are some approaches which can help: Unit testing Write unit tests where possible. Solely relying on manual testing or finding bugs in the wild is hit-and-miss. Use frameworks Rather than rolling your own and risking the introduction of bugs, use established frameworks where possible. Prefer CSS/high-level languages Where you can cede ...


10

Enums have nothing to do with OOP, and JavaScript doesn't have enums. Instead, enums are used whenever there is a choice between a fixed set of values. For example, a boolean is a choice between true and false, which could be implemented as enum Bool { False, True }. In a GUI library, we might have an enum for alignments: enum HAlignment { LEFT = -1, CENTER ...


5

It's not about an outsider being able to change it. Here is a better example which illustrates the usefulnes of closures: var getCodeForUser = (function(username){ var secureCode = CalculateCodeFor(username); return function () { return secureCode; }; })("ScrollingMarquees"); getCodeForUser(); // Returns the secureCode In this case the ...


5

In terms of speed of execution they are the same. The compiled/interpreted code is going to do a test if nodetype == 3. If yes it executes a return statement next. If no it executes the other return statement next. Both cases are going to execute a return statement next. In terms of stack usage, they are the same. In both examples shown, it only drops ...


4

Your proof that you will always reach a previously used 'i' appears to imply that your mapping function is topologically mixing. This in turn suggests that your function is probably chaotic. If it is chaotic, then by definition there is no faster way of producing the result than iterating your mapping function. Looking at it less rigorously, the modulus ...


4

I think the operations you're doing are variations of SQL joins, because in most cases you're taking tuples of data, picking a kind of value from each one to associate, and returning the combination(s) that could be linked. I'd suggest you look into the jargon, algorithms, and research-papers of RDBMs systems as your first place to mine for more ...


3

Even if your language doesn't require compilation, you'll probably use some kind of IDE or development tools, that can give much better support for something like an enum than just for strings. If you use an enum like object literal in javascript for example, your editor will give you code completion and your code checker like JSHint or JSLint will warn ...


3

Given a variable which needs to have a scope beyond a single function call, yet be accessible by only one function, I believe it's okay to not introduce a new scope just for that one function as long as you don't make the variable global. In other words, I'd put this variable at module scope. You should already have some kind of module system which prevents ...


3

Technically, there is no quicker or slower alternative: it's the same request to the same resource, and the processing and sending of the image would probably take much more time than the displaying on client side. The only difference is the perception by the user, with a major difference between <img> and background-image. If <img> doesn't ...


3

As a professional mathematician I see in Javscript's sameness operator == (also called "abstract comparison", "loose equality") an attempt to build an equivalence relation between entities, which includes being reflexive, symmetric and transitive. Unfortunately, two of these three fundamental properties fail: == is not reflexive: A == A may be false, e.g. ...


3

From what you've described, you don't need a circular reference at all, so the best thing you can possibly do is get rid of it completely. in Column class I have a build function which return a string by combining table and column like "tableA.colB". That why column need to know what table it belong to.These entities are used for building SQL statement. ...


3

Is that a good thing or does that mean I am doing something wrong? Without any more information and a concrete code sample, no, writing a DSL is not necessarily a bad thing. A DSL is a common design pattern if you have a lot of common logic. At some point, you may want to take it further and actually write it as a proper DSL.


2

By wrapping the whole thing in an IIFE, you can create private variables and functions. This way, the exposed API can wrap the private functions any which way, and persist would only be called when using the public delete: var obj; (function() { function realDelete(id) { delete obj.users[id]; //maybe some other code (ui manipulation) ...


2

Why is this not done by default? For some reason, it wasn't done in the earliest versions of the language we now know as Javascript. Unfortunately, changing it now would break tons of existing code, so we're stuck with it. Why not make this an "opt-in breakage" like strict mode? Probably because strict mode is focused on a small set of the most ...


2

Are you talking about the use of <script>-elements to store HTML templates? The purpose of the script element was to embed scripts, but the HTML spec does not mandate which languages should be supported by the browser. In case the specified language is not supported by the browser, the default behavior (mandated by HTML) is simply to hide the text. ...


2

It is conventional in recursive functions to write the base case first. That means your first option. Your second option is testing for the inverse of the base case, which is confusing to read. Writing the base case first makes little difference in this particular case, but in the general case if you were to use pattern matching, or a switch statement, or ...


2

The point of such enum might be to provide Js Api (goog) a set/bundle of allowed tags. Which ones? The ones defined by W3C HTML 4.01 (check out enum's documentation). So it's settings boundaries. Might or might not be this the real goal, however it would works fine for such purpose. If you know how Javascript works, what code is doing is defining an array ...


2

One tool that hasn't been yet mentioned is simple, file-local or project-wide text search. It sounds simple, but when you include some regular expressions you can do some basic to advanced filtering, e.g. search for words located in documentation or source code. It has been an effective tool for me (besides static analyzers), and given your project size of ...


2

From a speed point of view, your Computer is most likely much faster than the server you have for your website/web-application, so it would make more sense to resize on the client, than on the server. I have a Java application where I change certain color pixels to another color, and then size the image down, and that total operation takes about 0.005 ...


2

If disc space is the only constraint then it doesn't matter which end resizes the image. The client might do it slightly slower than the server or the server may be overloaded with clients sending many images so its a judgement call for you to make based on expected usage patterns. However, there is one good reason for resizing on the client: network ...


2

As @Adam says, several of these are lambda expressions; in particular the ones with the =>. (1) someFuncion( () => callBack(someParameters) ) This is an invocation passing a lambda that is a closure; it is closing over someParameters, which must have been defined elsewhere in the context this snippet finds itself. (2) someFuncion( parameter ...


2

Probably because that's how regular expression flags are handled in most text editors and other programming languages. A lot of things about regular expressions make more sense when you're using them in a text editor such as vim, where almost all actions are keyboard-driven. For example, in vim if I type :s/foo/bar/g and press enter, all instances of foo ...


1

In your particular contrived example, there is no difference at all. If you only ever use a variable by value, and assign it a compile-time value, it matters not whether you use a closure or not. In fact, the closure may be slower, depending on your browser's JavaScript implementation. This contrived example is simply too simple -- there's no reason to ...


1

People often get excited talking about what closures can do, and they forget to talk about what you should do with closures. Your particular example builds on the previous example, which showed closures acting like poor man's objects. This is far from the best example of idiomatic closure use, but it is familiar to object oriented programmers, which I ...


1

This has not so much to do with Node.js but more with how zmq works. If you read the paragraph about ZMQ_REQ there is this part: This socket type allows only an alternating sequence of zmq_send(request) and subsequent zmq_recv(reply) calls. So a worker that receives a request can only receive an other request once the first request has gotten a reply. ...


1

Also, don't be afraid tot tell me it's a silly idea. It's a silly idea :P And here's why: Data caps. The primary hog when it comes to data are things that happen in the background without any user's consent. You wouldn't want to be leeching off a person's data. Battery and heat. Of course, processing takes up resources and resources doesn't come free, ...


1

Answering my own question: I have made some tests now. It looks like it is not possible to do this with that object in only 1 objectStore. An other example object which would work: var myObject = { "ordernumber": "123-12345-234", "name": "Mr. Sample", "shipping": {"method": "letter", "company": "Deutsche Post AG" } } ...



Only top voted, non community-wiki answers of a minimum length are eligible