Tag Info

Hot answers tagged

188

I'm going to make an argument for == Douglas Crockford which you cited is known for his many and often very useful opinions. While I'm with Crockford in this particular case it's worth mentioning it is not the only opinion. There are others like language creator Brendan Eich who don't see the big problem with ==. The argument goes a little like the ...


77

It turns out that jQuery uses the construct if (someObj == null) { // do something } extensively, as a shorthand for the equivalent code: if ((someObj === undefined) || (someObj === null)) { // do something } This is a consequence of the ECMAScript Language Specification ยง 11.9.3, The Abstract Equality Comparison Algorithm, which states, among ...


47

No, item.AddTo(items) it is not more natural. I think you mix this up with the following: t3chb0t.Add(item).To(items) You are right in that items.Add(item) is not very near to the natural english language. But you also don't hear item.AddTo(items) in natural english language, do you? Normally there is someone who supposed to add the item to the list. Be ...


24

Client-side Javascript cannot be secured - ever. If the browser can run it, that means that the Javascript instructions are 100% available to anyone who wants them. You can compress and obscure the Javascript. That is merely an obstacle that any talented developer can get around with some extra investment of time. So, a novel algorithm cannot be ...


16

If you want someone to be able to run a program, you have to give them the program. (Duh!) If you want the CPU to execute the program, the CPU has to understand the program. (Duh again.) Computers are stupid, much stupider than humans. Ergo, if the CPU can understand the program, a human can too. One way would be to give your users both the program and the ...


11

Checking values for null or undefined is one thing, as has been explained abundantly. There's another thing, where == shines: You can define comparison from >= like so (people usually start from > but I find this more elegant): a > b <=> a >= b && !(b >= a) a == b <=> a >= b && b >= a a < b and a <= b are ...


11

First thing : A pure JS code in a separate file will be cached, reducing the amount of data transfered on each request. Unless you have a very small, page specific JS code, you shouldn't inline it. Other valid reasons are already pointed in Mike's answer. If you have to pass some values from the server to the JS, what you can do instead of injecting JSP ...


7

I had a similar problem where we needed to display information about cars and other products that was structured like a tree (car brand -> model -> type -> build year). Even if the browser would hold that much data (not sure about that) it would take forever to download it. What I did: Load the first level and load other levels via Ajax only when actually ...


7

Yes, I've run across a use case for it, namely when you're comparing a key with a numerical value: for (var key in obj) { var some_number = foo(key, obj[key]); // or whatever -- this is just an example if (key == some_number) { blah(); } } I think it's a lot more natural to perform the comparison as key == some_number rather than as ...


7

There are two primary reasons: Separation of concerns - by having the code in a separate file it is separate from the HTML and may be changed without touching the HTML file. If used in multiple places it allows for a single place to make the change. DRY - Don't Repeat Yourself By having it in a separate file you don't have to repeat the code in several ...


5

There is no 100% solution to your problem. A user can use a wide range of strategies to bypass any possible action: delete cookies, use a VPN, even change its device (mobile phone, pc, laptop). You mentioned them. What are the options I have? That said, IP adress and a browser fingerprint is your best bet. There are libraries for JAVA (and probably ...


5

Short answer: yes. A stream in the context of software is simply a sequence of bytes. What those bytes represent is up to you: it could be ASCII text, Unicode characters, a JPEG image, serialized object, anything your heart desires. The important thing is that the producer and consumer of the stream agree on the format. Furthermore, a stream is not ...


4

@valenterry is quite right about the problem of separation of concerns. Classes should know nothing about the various collections which might contain them. You would not want to have to change the item class each time you create a new kind of collection. That said... 1. Not Java Java is no help to you here, but some languages have more flexible, ...


3

PHP and Java are processed Server-Side. JavaScript is processed by the web browser. If you were processing in real-time with PHP, you would need a piece of JavaScript (such as an AJAX POST) to send the entire chunk of code to the server for re-processing, wait for a return and then replace the entire display with newly formatted text every time a user ...


3

PHP or Java Servlets are purely server-sided technologies. They can't do anything unless the user clicks on a link. When you want any interactivity while the website is displayed, you need Javascript, at least for an AJAX call to a PHP/JSP backend. When you want an application like a code editor the user is interacting with very closely, you want as much of ...


3

I ran across a pretty useful application today. If you want to compare padded numbers, like 01 to normal integers, == works just fine. For example: '01' == 1 // true '02' == 1 // false It saves you removing the 0 and converting to an integer.


3

You need to do it from time to time in development to check that it works correctly and isn't adding any additional bugs. It shouldn't be a step that you're using just in your release process, because that should be happening post-testing. You shouldn't be doing it for every small change you make, but from time to time you should absolutely be checking ...


3

I think that both of the testing method proposed so far are bad. omouse suggest exposing validationRules an internal implementation detail. In doing so, he is suggesting that you test the implementation when you should be testing the interface. See this blog for discussion. The op then proposed returning the number of added rules. But as far as I can tell ...


3

@Mike and @X.L.Ant's answer already provide some excellent points. I can provide one other point that hasn't been mentioned yet. Ease of Editing & Tool Use - All kinds of things work better when you put your JS in a separate file. Here are some examples: JSHint/JSLint and other static code analysis tools Code beautification like JSBeautify Code ...


2

If you attach your event listener like this: $(document).on({ click: function () { // ... } }, '.some-class'); Then your event listener will not "leak" as you put it. You can redraw, add additional elements with .some-class and they will all inherit the event listener. This has the added benefit of only registering one event listener ...


2

I know this is a late answer, but there seems to be some possible confusion about null and undefined, which IMHO is what makes == evil, more so that the lack of transitivity, which is bad enough. Consider: p1.supervisor = 'Alice'; p2.supervisor = 'None'; p3.supervisor = null; p4.supervisor = undefined; What do these mean? p1 has a supervisor whose name ...


2

Most smartphone app programming APIs offer some kind of "web view". A widget which is a browser window embedded in the app itself. When you create such a web view as a full-screen element and load a local copy of the web application into it, it should (usually) work. However, the app you posted is not completely client-sided. It also has a server-sided ...


2

The only fool proof way is to force users to identify themselves with an authentication mechanism. Identification is the "Who are you?" question, and the answer can be a user name, real name, email address, browser finger print, etc. Authentication is "Who are you and prove it!". In computer security this is implemented by requiring the user to supply a ...


2

With your extension method you still have to call Add() so its not adding anything useful to your code. Unless your addto method did some other processing there's no reason for it to exist. And writing code that has no functional purpose is bad for readability and maintainability. If you leave it non generic the problems become even more apparent. You now ...


2

You definitely should be unit-testing addErrorRule. You can do this by testing for the side-effects of the addValidationRule call. Make sure there is a validation rule added to the right list (errors, not warnings). Yes, you must expose the validationRules variable to be able to unit-test addErrorRules. Here's example code for how the unit test might look ...


1

I think you are obsessing with the word "add". Try looking for an alternative word instead, such as "collect", which would give you a more English-friendly syntax with no change in pattern: items.collect(item); The above method is exactly the same as items.add(item);, with the only difference being different word choice, and flows very nicely and ...


1

Just to give a quick and easiest example using jQuery: <span id="main"></span> <button onclick="add();">Click Me</button> function add(){ $("#main").html("<h1>Hello</h1>"); } The <span> will be populated with <h1>Hello</h1> on button click You can run the above example here: ...


1

Given your desired total and a length, the most balanced array gets generated where the value of each entry is (total / length) or ((total / length) + 1); function MakeSum( total, length ) { var ret = []; var portion = Math.floor(total / length); var remainder = total % length; var running_total = 0 if ( portion % 2 ) { if(portion > 2) ...


1

Advantages: The display knows nothing about the business logic. You can't display something you know nothing about. Trying to invert control so the business logic calls some sort of display API just turns part of the business logic into display logic and intertwines the two. Then your business logic will break if the display API or display logic ever ...


1

Besides those facts mentioned already, I would like to add that at the heart of all major browsers there is a powerful EcmaScript/JavaScript engine, incredibly improved by years and years of evolution of the Web. Therefore all major browsers natively speak this language and are capable of running programs in it in the most efficient way. Why in the world ...



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