• jQuery event binding internal

    The event binding in jQuery is very interesting. It is designed with several goals. 1. It should works consistently across browsers. 2. It should allows attaching more than one handler function to a event. The event registration information is saved in a jQuery.data(elem). In jQuery.cache actual data store for all these data. A piece of data in the cache can be assigned to the element. Below is some pesudo code to demonstrate the data structure

    function fn(..){..}
    fn.guid = ..
    
    
  • shadow copy in jQuery

    We can use jQuery to copy(clone) an object to a new object, so the old object and the new object can evolved independently.


    //shadow copy object
    var oldObj = { name: "fred" };
    var newObj = jQuery.extend({}, oldObj);
    newObj.name = "jeff";
    alert(oldObj.name); //fred
    



    To do similar thing for array, we can use [].slice() method


    //clone array
    var newArray = arr.slice(0);
    
  • copy array in javascript

    var x = [1, 2];
    var y = x.slice(0);
    y[0] = 100; //y --> [100, 2]
    alert(x[0]); //1
    ​

    The new copy evolve independently of the old copy.

  • Easy Setter Functions in jquery

    One of the new feature in jQuery 1.4 is setter function. For a while now, you’ve been able to pass a function into .attr() and the return value of that function is set into the appropriate attribute. This functionalilty has now been extended into all setter methods: .css(), .attr(), .val(), .html(), .text(), .append(), .prepend(), .before(), .after(), .replaceWith(), .wrap(), .wrapInner(), .offset(), .addClass(), .removeClass(), and .toggleClass(). Addtionally, for the following options, the current value of the item is passed into the function as the second argument: .css(), .attr(), .val(), .html(), .text(), .append(), .prepend(), .offset(), .addClass(), .removeClass(), and .toggleClass().

    $('a[target]').attr("title", function(i, currentValue){
      return currentValue+ " (Opens in External Window)";
    });
    
    
  • how to test whether a object has member

    Object.prototype.hasMember = function (memberName)
      {
        return memberName in this;