why 'event.preventDefault();' is better than 'return false;'

event.preventDefault()

If this method is called, the default action of the event will not be triggered. You should try and use this instead of "return false;"

Return false is actually doing three things when invoked:

  1. It invokes event.preventDefault(), which is what you wanted.
  2. It invokes event.stopPropagation(), which may be what you wanted. But any other click events in (for instance) wrapping divs won't get fired.
  3. It stops callback execution and returns immediately when called.

Another example:

$('x').click(function(e){
  // custom code
  //but this line causes a runtime error
  return false;
});

Whoops, if you throw an error before you hit return false, you are rolling the dice, because the return false never executes. What happens if the click function is attached to a form submit button? You will submit the form. That's probably not what you want.

Now try this:
$('x').click(function(e){
  e.preventDefault();

  // custom code
  //but this line causes a runtime error
});

Because you placed the preventDefault() invocation at the top of the function, you have shut down the default behavior of the element. In other words, even though you get a runtime error, you haven't screwed up your form button, and the form doesn't submit.

The meaning of file permissions

Linux file permissions appear to be cryptic, but they're straight forward. "775" has an immediately intuitive meaning, if you are aware of two things:

1. Each number represents the permission for the file owner, for the group owner, and for everyone else.

2. The number represents the permission for that owner.

The permission number is arrived at by assigning a numeric value to each allowed permission:

= read (r)
2 = write (w)
1 = execute (x)
0 = no permission (-)

In the case of a file with permission 751, it can be deciphered to mean:

The owner of the file has permission 7, which is read (4) + write (2) + execute (1).

The group owner has permission 5, which is read (4) + execute (1).

Everyone else has permission 1, which is execute (1);

What is a javascript prototype?

What is a prototype in javascript?

A prototype is an object. Every object in javascript has it as a property. It can be considered the template for that object, and any other objects will use it to  inherit properties.

When you want to dynamically add properties or methods to a javascript 'class', you will use the prototype:

function bank(balance)
{
   this.balance = new_balance;
   this.getBalance = function(){ return this.balance; }
}

bank.prototype.deposit = function(amount){ this.balance += amount; }

var my_account = new bank(0);
my_account.deposit(10);
alert("New balance is " + my_account.getBalance(); }

So the object prototype is found in object.prototype?

Uh, no. Although you will use object.prototype to modify the object's prototype, it isn't technically found there. If you want to access the actual prototype object, you need to look elsewhere. The object.prototype property is the prototype that is assigned to all instances of the object,

The true prototype of an object is held in the internal [[Prototype]] property. ECMA 5 introduced a standard accessor Object.getPrototypeOf(object) which works in Firefox, Safari, Chrome and IE9. All browsers (except IE -- surprise!) support the non-standard accessor __proto__. Finally, we can reach it through the object’s constructor via the prototype property.

var a = {};

//Fails in Opera or IE<=8
var myPrototype = Object.getPrototypeOf(a); //returns [object Object]

//Fails in IE
var myPrototype = a.__proto__; //returns [object Object]

// works all browsers
//(but only if constructor.prototype has not been replaced and fails with Object.create)
var myPrototype = a.constructor.prototype; //returns [object Object]

Can any object be a prototype?

Hell, yes.

The $64,000 question: so what?

The prototype is the seed for all other objects.  It's a way to dynamically change the internals of any javascript 'class'. When you update the prototype, any previously instantiated objects gain the update.

var dog = { }

var my_dog = new dog();
alert(dog.name); // undefined

dog.prototype.name = 'sparky';
alert(my_dog.name); //'sparky';

dog.prototype.setName = function(name){ this.name = name; }
dog.prototyp.getName = function(){ return this.name; }

my_dog.setName('fido);
alert(my_dog.getName()): // 'fido';

My feelings about the code debate

A graphical representation about how I feel about the ongoing argument about the iPhone vs Android, or Windows vs Mac, or PHP vs Python et al

funny graphs - Apathy Charted