1. Basic object: Number,string,date,array,error,regexp,math,boolean
PS: I basically use Java to write code, often write number n = new number (2) such as two code, time JavaScript is weak type, unified use Var to define, var n = new Number (2)
A 2.Number object that retains the specified decimals using the Tofixed function, and also has the same properties as the Toprecision function, which has an exponential function (detailed description)
function Fixnumber () { varnew number (10/3); Alert (n.tofixed (2)); // Keep 2 decimal places}
3.String objects, Basic and Java-like, common methods are substring,indexof,replace, but there is no trim, in some new browsers are supported ExtJS also defines a string object, This object is trim so use ExtJS when used with ease. Ps:javascript because of the encoding problem, the length of the string uniform is one character, for the information to be saved to the database should be judged by the number of bytes, to prevent the field space is not enough, The rule is non-Chinese length according to 1, Chinese length according to 3 count
function substr () { var str = "Old King Xie Tang Qian Yan, fly into ordinary people's home"; Alert (str.substring (0,8) + "Length:" + str.substring (0,8). length);}
4.| | Operator
Represents or operations, this and Java | | A little different, JavaScript's | | Sometimes the Boolean type is not returned, and the arithmetic rule op1| | OP2, if OP1 is true, returns OP1, if OP1 is False, returns the Boolean operation of Op2,javascript even if non-boolean types can be calculated, so | | There is a new usage, and this usage is often seen in the source code, where the default value is ExtJS to ensure that there are no parameter errors
function Ordemo () { var op1 = 0; var op2 = {x:0}; var true ; var result = op1| | op2.x| | OP3; // the value of OP3 is returned when the front op1,op2 is false alert (result);}
5. Type conversion Techniques
String precedence
function TransDemo1 () { var s = "1" + 5; alert (s); // 5 will convert the string first var a = ["1", "2", "3"]; + ""); // First call the Array.tostring function and then the "" Connection }
The object's key is always a string
function ObjkeyDemo1 () { var obj = {ten: "ObjkeyDemo1"} alert (obj["]);}
6.for...in statements
For...in syntax can walk out the key of object, the example of the dump function cited in the book, all the attributes of the DIV element are listed in the table element, so look at the effect together, click on the test will be the DIV element variable, the output Attribute table page
functiondump (obj) {vars = window.open ("", "Dump", ""); varresult = []; for(Keyinchobj) {Result.push (key); } result.sort (); varhtml = "<table border=1>"; for(vari=0;i<result.length;i++) {HTML+ = "<tr>"; HTML+ = "<td>" + result[i] + "</td><td>" + obj[result[i "] +" </td> "; HTML+ = "</tr>"; } HTML+ = "</table>"; S.document.body.innerhtml=html;}functionGetobjtodump () {varDiv1 = document.getElementById (' Test '); Dump (DIV1);}
Ext. NOTES: JavaScript Basics