Monday, October 15, 2012

When do you need to send AutoRelease message to an object

If the object is created with convenience method, like [NSMutableArray arrayWithCapacity], then the convenience constructor already sends the AutoRelease message to the object when it is created, so when the auto release pool or block is ended, it will send the release message to the object.

If the object is created with the alloc or copy constructor, and the object is returned to caller as a method return value or output parameter, then you should call AutoRelease message on the object. The reason is the receiver does not create the object, so he is not the own of the object and is not responsible to release the object. While after the object is returned by the method, then the creator of the object lose the control of the object and cannot actively release the object. The only limited control the original creator has on the object is sending autorelease message to it before relinquishing the control of the object, and that will be sure the object gets released sometime when the autorelease pool is closed.

However, in both cases, if the autoreleased object is created on a long-living auto release pool, then the object will last for long time and that will cause the object dose not be released quickly.

Sunday, October 14, 2012

javascript function scope sample

Javascript scope is used to enclose the internal logic and only expose the necessary public information to external. Basically, within a object or function scope, only properties or function  defined on the object are visible to external as public info through the object. Any local variables defined with var, or inner function are only available to internal logic.

var globalFunction = function () {    // global, can be accessed anywhere.
    alert('hello world');
}

var containerFunction = function () { // globale namespce, can be accessed anywhere.
   
    var subFunction = function () {    // private namespace method
        alert("I'm Private");
        globalFunction();              // We can access global Function here 2 levels deep.
    }
   
    containerFunction.test = function () {  //public namespace method
        alert("test");
    }

    callableFunc = function () {   //global method, because variable is global
        alert("callable");
    }

    function localFunc(){   //only visible to local
       alert("local");
    }

    varible ="8";  //unlike function, new varibles without var gets added to global window object

    globalFunction();                 // We can access global Function here 1 level deep.
    subFunction();                     // We can access subFunction inside containerFunction.
    localFunc();
    alert(varible);
}

containerFunction();
containerFunction.test();
callableFunc();
alert(varible);

//failure cases
//test();  //test is not visible in global scope
//subFunction(); //namespace private method and only exists inside containerFunction
//containerFunction.callableFunc(); //callableFunc is not belonging to namespace, it is global
//localFunc() //not visible

Note, unlike 

javascript self invoking function parameter

The sample javascript self invoking function is defined as follows
(function(parmeter1){
    alert(parameter1);
}("hello world");

Notice the data in the last parentices is the actual caller that will invoke the function, which passes the argument's value as "hello world".
Inside the function definition, the parameter1 is replaced by the string "hello world", and shows the alert.

Wednesday, October 10, 2012

addSubView and pushViewController difference

For ios project, addSubView and PushViewController can be used to display a new view. However, they are quite different.

addSubView is a instance method of View class, and it adds a view item into itself. If the subview occupies the whole screen, then it has the same effect of display a new view.

pushViewController is a instance method if UINavigationViewController, so it can only be used on UINavigationViewController object, it has a better isolation than previous method, as it does not add the new view as a subview in the parent view.

Tuesday, October 9, 2012

Array returned from jQuery selector and html element

When using jquery id selector, it will only return a single object. However, similar to other jquery selector, the returned object is wrapped in a jquery object array. Note all jquery selector methods return an array, as it is always possible to match more than one elements for the selector.
If you call a jquey method to retrieve the attribute on the returned array, like val(), it actually returns the attribute of the first element. When the object is selected by id, it is perfectly fine, as there is only one element. However, if the array contains multiple elements, it does not have much sense to just return the first element's value when calling the method on the array. Although, in both cases, setting the value is fine, as it makes sense to set the same value for all elements or a single element.
In order to return a particular jquery element from a multiple element array, call jQuery('selector:eq(index)') method. Remember, the returned value from the method is still wrapped in a jquery array, the only difference is the array will only contain a single jquery object.
In order to return a particular htmlElement, call jQuery('selector').get(index). The returned object is a single html object, (not a array, not a jquery object), and it has all the applicable attribute defined for html element such as value attribute.