Most Popular
1500 questions
3166
votes
13 answers
event.preventDefault() vs. return false
When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I'll use jQuery in the examples, but this applies to plain-JS as well:
1. event.preventDefault()
$('a').click(function (e) {
…
RaYell
- 68,096
- 20
- 124
- 150
3133
votes
32 answers
"Least Astonishment" and the Mutable Default Argument
Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue:
def foo(a=[]):
a.append(5)
return a
Python novices would expect this function to always return a list with only one element: [5]. The…
Stefano Borini
- 132,232
- 95
- 283
- 413
3130
votes
33 answers
Initialization of an ArrayList in one line
I wanted to create a list of options for testing purposes. At first, I did this:
ArrayList places = new ArrayList();
places.add("Buenos Aires");
places.add("Córdoba");
places.add("La Plata");
Then, I refactored the code as…
Macarse
- 89,619
- 44
- 170
- 229
3125
votes
20 answers
Echo newline in Bash prints literal \n
How do I print a newline? This merely prints \n:
$ echo -e "Hello,\nWorld!"
Hello,\nWorld!
Sergey
- 43,906
- 25
- 84
- 124
3123
votes
41 answers
Why is "using namespace std;" considered bad practice?
I've been told by others that writing using namespace std; in code is wrong, and that I should use std::cout and std::cin directly instead.
Why is using namespace std; considered a bad practice? Is it inefficient or does it risk declaring ambiguous…
akbiggs
- 31,959
- 6
- 24
- 36
3120
votes
44 answers
JavaScript closure inside loops – simple practical example
var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
// and store them in funcs
funcs[i] = function() {
// each should log its value.
console.log("My value: " + i);
};
}
for (var j = 0; j < 3; j++) {
// and now…
nickf
- 520,029
- 197
- 633
- 717
3119
votes
31 answers
Git refusing to merge unrelated histories on rebase
During git rebase origin/development the following error message is shown from Git:
fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef
My Git version is 2.9.0. It used to work fine in the previous version.…
Shubham Chaudhary
- 42,546
- 9
- 73
- 80
3118
votes
50 answers
Detecting an undefined object property
What's the best way of checking if an object property in JavaScript is undefined?
Matt Sheppard
- 113,819
- 46
- 107
- 128
3113
votes
20 answers
What is the difference between Python's list methods append and extend?
What's the difference between the list methods append() and extend()?
Claudiu
- 216,039
- 159
- 467
- 667
3106
votes
12 answers
How do I copy a folder from remote to local using scp?
How do I copy a folder from remote to local host using scp?
I use ssh to log in to my server.
Then, I would like to copy the remote folder foo to local /home/user/Desktop.
How do I achieve this?
Slasengger
- 31,337
- 3
- 12
- 10
3100
votes
96 answers
How do you disable browser autocomplete on web form field / input tags?
How do you disable autocomplete in the major browsers for a specific input (or form field)?
Brett Veenstra
- 46,426
- 17
- 69
- 85
3091
votes
37 answers
How do I pass a variable by reference?
Are parameters are passed by reference or value? How do I pass by reference so that the code below outputs 'Changed' instead of 'Original'?
class PassByReference:
def __init__(self):
self.variable = 'Original'
…
David Sykes
- 46,089
- 17
- 67
- 78
3091
votes
54 answers
How to stop EditText from gaining focus when an activity starts in Android?
I have an Activity in Android, with two elements:
EditText
ListView
When my Activity starts, the EditText immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I…
Mark
- 38,871
- 15
- 39
- 46
3089
votes
62 answers
How do I format a date in JavaScript?
How do I format a Date object to a string?
leora
- 177,207
- 343
- 852
- 1,351
3087
votes
43 answers
Add a column with a default value to an existing table in SQL Server
How can I add a column with a default value to an existing table in SQL Server 2000 / SQL Server 2005?
Mathias
- 31,973
- 7
- 23
- 33