Javascript proficiency
Chap 1
1. Use a namespace to protect your database.
YAHOO.util.Event.addListner(...);
Chap 2
2. Reference
Multiple variables can point to the same object. A reference can only point to a specific object, rather than another reference. Multi-layer references are not allowed.
However, when the string connection operation is executed, the result is always a New String object instead of the modified version of the source string.
Eg1.
var object=new Object();var objRef=obj;obj.oneProperty=true;alert(obj.oneProperty === objRef.oneProperty);
Eg2.
var items=new Array("one","two","three");var itemsRef=items;items=new Array("new","Array");alert(items!=itemsRef);
Eg3.
var item="test";var itemRef=item;item+="ing";alert(item!=itemRef);
3. Determine the input parameter type
If no parameter is provided, the type must be undefined.
if( typeof msg == 'undefined') {...}
1) typeof statement
This is a good solution when the variable is not of the object or array type. However, for a custom object, for example, a user cannot use this method for type check, because it only returns the object.
2) constructor
This property references the function originally used to construct the object.
// check numberif(num.constructor == String) num=parseInt(num); //check arrayif(str.constructor == Array) str=str.join(',');
3) Comparison between typeof and constructor
Variable |
Typeof variable |
Constructor |
{An: "object "} |
Object |
Object |
["An", "array"] |
Object |
Array |
Function (){} |
Function |
Function |
"A string" |
String |
String |
55 |
Number |
Number |
True |
Boolean |
Boolean |
New User () |
Object |
User |