Home

fixed object id of ruby objects

0 => False 1 => 0 (Fixnum) 2 => True 3 => 1 (Fixnum) 4 => Nil 5 => 2 (Fixnum) 6 => Undefined

Ways to write your own jQuery Plugin

If you want to extend jQuery element collection, like:
$("input[type=checkbox]").check();
$("input[type=radio]").uncheck();
Then you can do like this:
jQuery.fn.extend({
  check: function() {
    return this.each(function() { this.checked = true; });
  },
  uncheck: function() {
    return this.each(function() { this.checked = false; });
  }
});
If you want to add methods for jQuery namespace, like:
jQuery.min(2,3); // => 2
jQuery.max(4,5); // => 5
You can do like this:
jQuery.extend({
  min: function(a, b) { return a < b ? a : b; },
  max: function(a, b) { return a > b ? a : b; }
});
But in case of doesn't collide with other libraries that might use the dollar sign, it's a best practice to pass jQuery to a self executing function (closure) that maps it to the dollar sign.so it can't be overwritten by another library in the scope of its execution.
(function( $ ) {
  $.fn.myPlugin = function() {

    // Do your awesome plugin stuff here

  };
})( jQuery );
Best practices:
  1. Always wrap your plugin in a closure: (function( $ ){ /* plugin goes here */ })( jQuery );
  2. Don't redundantly wrap the this keyword in the immediate scope of your plugin's function
  3. Unless you're returning an intrinsic value from your plugin, always have your plugin's function return the this keyword to maintain chainability.
  4. Rather than requiring a lengthy amount of arguments, pass your plugin settings in an object literal that can be extended over the plugin's defaults.
  5. Don't clutter the jQuery.fn object with more than one namespace per plugin.
  6. Always namespace your methods, events and data.
  7. jQuery.fn is pronounced jQuery effin'
Reference link: http://docs.jquery.com/Plugins/Authoring

Trip of Javascript OO

Create Object:
function MyObject() {
  this.property = "x";
  this.method = function() {};
}
Useful functions in Object: if(A instanceof Array),  hasOwnProperty('property_name')
Prototype:
x_instance.prototype.new_method = function() {} This line will add new_method to all instances of same class of x_instance. (So. we can use this to monkey patch build-in methods or properties.)
Public and Private properties:
function() {
this.public_property = "x";
var private_property = "y";
this.public_method = function() {}
function private_method() {}
}
Static methods and properties:
function mycircle() { ... }
mycircle.aStaticProperty = true;  // static property
mycircle.aStaticMethod =function () { ... }; // static method
Use anonymous function to make a private scope:
var mycircle = (function () {
var privateStaticVariable = true;
function privateStaticMethod() { ... }
function publicConstructor() {
this.normalInstanceProperty = 1;
...
alert(privateStaticVariable);
};
publicConstructor.prototype.normalProperty = 2;
return publicConstructor;
})();
mycircle.publicStaticProperty = true;
mycircle.publicStaticMethod = function () { ... };
Singletons:
var mySingleton = {a:1, b:2,c:function(){}}
var aReference = mySingleton
Another way: http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html also the way coffee script version: http://coffeescriptcookbook.com/chapters/design_patterns/singleton
var singletonMaker = (function () {
var privateStaticVariable = true;
function privateStaticMethod() { ... }
var onlyInstance;
var returnedObject = {
getInstance: function () {
if( !onlyInstance ) {
onlyInstance = new PrivateConstructor();
onlyInstance.constructor = null; // Then you will never be able to New another this object.
}
return onlyInstance;
}
};
function PrivateConstructor() {}
PrivateConstructor.prototype = returnedObject;
PrivateConstructor.prototype.publicProperty = true;
...
return returnedObject;
})();
...
singletonMaker.foo = true;
var mySingleton = singletonMaker.getInstance();
alert(mySingleton.foo); //true
Sub-classes and class inheritance:
function House() {
this.name = "house"
this.tell_name = function() {
console.log(this.name)
}
}

function Villa(new_name) {
this.name = new_name || this.name
}

// Method 1: Villa.prototype.__proto__ = House.prototype; // this is another way.
// Method 2: Villa.prototype = House.prototype;
// Method 3: 
// function Villa() {
//   House.apply(this, arguments);
// }
// Method 4:
Villa.prototype = new House()
Villa.prototype.constructor = Villa
Villa.prototype.show_swimming_pool = function() {
  console.log("swimming pool")
}
v = new Villa("villa")
v.tell_name()
v.show_swimming_pool()
Javascript Objects Map: http://www.howtocreate.co.uk/tutorials/javascript/javascriptobject

Google PageSpeed Online

https://developers.google.com/pagespeed/ Online version, could give suggestion for your online website. Also has offline plugin for each browser. http://code.google.com/speed/page-speed/docs/using_chrome.html#Installing

15 Essential Javascript video tutorials

http://www.virtuosimedia.com/dev/javascript/15-essential-javascript-video-tutorials