Friday, November 16, 2007

Javascript in depth


function User (name, age) {
   this.name=name;
   this.age=age;
}

/* User is a reference to the constructor */
User.prototype.getName = function() { ...}
// add new function to object prototype
User.prototype = new Person();
// inherit all of Person object's method, in JS, we inherit from physical object;
//it is like that you get all properties/methods when creating a new Person object
User.cloneUser = function(user) { ...} // static method

var user = new User("Bob", 33);


- what is constructor?
It is same as other function, but is invoked using keyword 'new'. Two things happen when invoke using 'new'
a) the context is switched to the created object, i.e., 'this' refers to object user. Otherwise, if invoke without new, this variable refers to root object window.
b) The constructor's prototype property (the public accessible one) is assigned to the new instance's private prototype.
In a nutshell, all JS objects have a private prototype property. I say "private" because this property is not directly visible or accessible from the object itself (but Visible for constructor!!). When the runtime performs a lookup on a property, it first looks at the instance to see if that property is defined there. If it is not defined there, the runtime looks for the property on the object's private prototype. Prototypes can have their own private prototype, so the search can keep working its way up the prototype chain.

- HTML DOM load
The order of loading is:
1. HTML is parsed
2. External scripts/style sheets (include the one in header) are loaded
3. Scripts are executed as they are parsed in the document (** significant problem: the scripts in these two places won't have accessed to DOM.)
4. HTML DOM is fully constructed. (inline scripts are executed as they are encountered. DOM is partially constructed atm)
5. Images and external content are loaded
6. The page is finished loading.

- Boolean operators
In JS, a||b <=> a?a:b a&&b <=> a?b:a they are not necessary return true or false as in Java/C. Lots of thing are "true" in JS, in fact, only things that are "false" are the false Boolean, the numbers 0 and NaN, the empty string, null and undefined.

e.g., var e = e.chileNodes || e; e = e || window.e; opt.time = opt.time || 400;

Sunday, November 11, 2007

吴清源

蜗牛角上争何事,    

石火光中寄此身。    

随富随贫且欢乐,

不开口笑是痴人。

Thursday, November 8, 2007

CSS Hack


* html div.tableContainer { /* IE only hack */
   width:95%;
   border:1px solid #ccc;
   height: 285px;
   overflow-x:hidden;
   overflow-y: auto;
}


* html div.tableContainer table thead tr td,
* html div.tableContainer table thead tr th{
/* IE Only hacks */
   position:relative;
   top:expression(dojo.html.getFirstAncestorByTag(this,'table').parentNode.scrollTop);
}

html>body tbody.scrollContent {
   height: 262px;
   overflow-x:hidden;
   overflow-y: auto;
}

tbody.scrollContent td, tbody.scrollContent tr td {
   background: #FFF;
  padding: 2px;
}

tbody.scrollContent tr.alternateRow td {
  background: #e3edfa;
  padding: 2px;
}


<div class="tableContainer">
<table dojoType="SortableTable" widgetId="testTable" headClass="fixedHeader" tbodyClass="scrollContent" ...>
  <thead>


It is excerpted from dojo sortableTable test file, since IE doesn't support overflow(scrollbar) on tbody, it plays a trick by wrapping a scrollable div around table, and relatively position the thead to simulate the effect.

* html only visible to IE, and child selector html>body not visible to IE. Also note that the descendant selector is applied above.

Wednesday, November 7, 2007

MySQL

- MySQL has ENUM type, it is used to enforce the data integrity and de-normalize table. Those joins may add unnecessary complexity to your database.


create table reservations (
   reservation_id int unsigned auto increment primary key,
   ...
   seat_pref_description enum('Window', 'Aisle')
);

Should a user place a invalid value to this column, MySQL will block the incorrect entry and place NULL instead.

- CHAR and VARCHAR have maximum length 255 limitation, can followed by keyword BINARY, mean string comparison case sensitive. Longer string is stored as TEXT type or BLOB( for binary data).

- Two (or more) storage engines: INNODB, MYISAM. The default engine can be specified with parameter default-storage-engine in my.ini.
INNODB: support transaction safe, row-level locking and foreign key
MYISAM: faster, and support full-text searching

create table example(field1 int, field2 varchar(30)) engine=MYISAM;