Js function set

Source: Internet
Author: User
Tags natural logarithm

Js function set · String (String) 1. declare var myString = new String ("Every good boy does fine. "); var myString =" Every good boy does fine. "; 2. string connection var myString = "Every" + "good boy" + "does fine. "; var myString =" Every "; myString + =" good boy does fine. "; 3. truncates a string // truncates the character var myString = "Every good boy does fine, which starts with 6th characters. "; var section = myString. substring (6); // Result: "good boy does fine. "// truncate the 0th-bit start-to-10th-bit character var MyString = "Every good boy does fine. "; var section = myString. substring (11th); // Result: "Every good" // capture the character var myString = "Every good boy does fine from the second to the last 6th. "; var section = myString. slice (11,-6); // Result: "boy does" // The 4-character var myString = "Every good boy does fine is intercepted starting from the first digit. "; var section = myString. substr (6, 4); // Result: "good" 4. convert var myString = "Hello"; var lcString = myString. toLowerCa Se (); // Result: "hello" var ucString = myString. toUpperCase (); // Result: "HELLO" 5. String comparison var aString = "Hello! "; Var bString = new String (" Hello! "); If (aString =" Hello! ") {}// Result: true if (aString = bString) {}// result: false (two objects are different, although their values are the same) 6. returns the var myString = "hello everybody. "; // if the string cannot be retrieved,-1 is returned. if (myString. indexOf ("every")>-1) {}// result: true7. find the replacement string var myString = "I is your father. "; var result = myString. replace ("is", "am"); // Result: "I am your father. "8. special Character: \ B: backward character \ t: horizontal tab \ n: linefeed \ v: vertical Tab \ F: Paging character \ r: carriage return character \ ": Double quotation marks \ ': single quotation marks \: backslice lever 9. convert the character to Unicode encoded var myString = "hello"; var code = myString. charCodeAt (3); // returns the Unicode (integer) var char = String of "l. fromCharCode (66); // returns the character 10 with Unicode 66. convert the string to URL encoded var myString = "hello all"; var code = encodeURI (myString); // Result: "hello % 20all" var str = decodeURI (code ); // Result: "hello all" // encodeURIComponent () decodeURIComponent () 11. convert string to bas E64 encoding // base64Encode () base64Decode () usage same as above // ------------------------------------------------------------------- Number type (Number) 1. declare var I = 1; var I = new Number (1); 2. conversion between strings and numbers var I = 1; var str = I. toString (); // Result: "1" var str = new String (I); // Result: "1" I = parseInt (str); // result: 1 I = parseFloat (str); // result: 1.0 // Note: parseInt, parseFloat forcibly converts a string similar to "32G" to 323. determine whether it is a valid number var I = 123; var str = "String"; if (typeof I = "number") {}// true // some methods (such as parseInt and parseFloat) a special value NaN (Not a Number) is returned. // please note [note] In point 2nd. This method is Not suitable for determining whether a string is Numeric !! I = parseInt (str); if (isNaN (I) {} 4. number comparison // This knowledge is the same as [String comparison] 5. convert decimals to Integers var f = 1.5; var I = Math. round (f); // result: 2 (rounding) var I = Math. ceil (f); // result: 2 (returns the smallest integer greater than f) var I = Math. floor (f); // result: 1 (returns the largest integer smaller than f) 6. formatted to display the number var I = 3.14159; // formatted as a floating point number var str = I. toFixed (2); // Result: "3.14" // formatted as a floating point number of the five digits (from left to right, five digits, not enough) var str = I. toPrecision (5); // Result: "3.1415" 7. convert X-in numbers // not very familiar -. -var I = parseInt ("0x1f", 16 ); Var I = parseInt (I, 10); var I = parseInt ("11010011", 2); 8. random Number // return any decimal point between 0 and 1 var rnd = Math. random (); // returns any integer between 0 and n (excluding n) var rnd = Math. floor (Math. random () * n) // --------------------------------------------------------------------------- · Math object 1. math. abs (num): returns the absolute value of num 2. math. acos (num): returns the arc cosine of num 3. math. asin (num): returns the arcsin value of num 4. math. atan (num): returns the arc tangent value of num 5. math. atan2 (y, x): returns the arc tangent value of the operator whose y is divided by x. 6. math. ceil (num): returns the smallest integer greater than num 7. math. cos (num): returns the cosine of num 8. math. exp (x): returns the number 9 of x power based on the natural number. math. floor (num): returns the maximum integer 10 less than num. math. log (num): returns the natural logarithm of num 11. math. max (num1, num2): returns a large value of 12 in num1 and num2. math. min (num1, num2): return a smaller 13 in num1 and num2. math. pow (x, y): returns the value 14 to the power of y of x. math. random (): returns a random number between 0 and 1. math. round (num): return the value 16 after num rounding. math. sin (num): returns the sine of num 17. math. sqrt (num): returns the square root of num 1. 8. math. tan (num): returns the tangent of num 19. math. e: Natural Number (2.718281828459045) 20. math. LN2: natural logarithm of 2 (0.6931471805599453) 21. math. LN10: 10 natural logarithm (2.302585092994046) 22. math. LOG2E: log 2 is the base natural number (1.4426950408889634) 23. math. LOG10E: The base Natural Number of log 10 (0.4342944819032518) 24. math. PI: π (3.141592653589793) 25. math. SQRT1_2: the square root of 1/2 (0.7071067811865476) 26. math. SQRT2: square root of 2 (1.4142135623730951 )//------------------------------- ---------------------------------------- · Date type (Date) 1. declare var myDate = new Date (); // The current system time var myDate = new Date (yyyy, mm, dd, hh, mm, ss ); var myDate = new Date (yyyy, mm, dd); var myDate = new Date ("monthName dd, yyyy hh: mm: ss "); var myDate = new Date ("monthName dd, yyyy"); var myDate = new Date (epochMilliseconds); 2. obtain a part of the time var myDate = new Date (); myDate. getYear (); // obtain the current year (2 digits) myDate. getFullYear (); // Obtain the complete year (4 digits, 1970 -????) MyDate. getMonth (); // obtain the current month (0-11, 0 represents January) myDate. getDate (); // obtain the current day (1-31) myDate. getDay (); // obtain the current day X (0-6, 0 represents Sunday) myDate. getTime (); // obtain the current time (milliseconds starting from 1970.1.1) myDate. getHours (); // obtain the current hour (0-23) myDate. getMinutes (); // get the current number of minutes (0-59) myDate. getSeconds (); // obtain the current number of seconds (0-59) myDate. getMilliseconds (); // get the current number of milliseconds (0-999) myDate. toLocaleDateString (); // obtain the current date myDate. toLocaleTimeString (); // obtain the current time myDate. toLocaleStr Ing (); // obtain the date and time 3. calculate the previous or future time var myDate = new Date (); myDate. setDate (myDate. getDate () + 10); // The current time plus 10 days // similar methods are basically the same, starting with set. For details, refer. calculate the offset var I = daysBetween (beginDate, endDate) of two dates; // returns the number of days var I = beginDate. getTimezoneOffset (endDate); // returns the number of minutes. check the valid date // checkDate () only allows "mm-dd-yyyy" or "mm/dd/yyyy" Two formats of date if (checkDate ("2006-01-01 ")) {} // Regular Expression (check for four types of expressions: yyyy-mm-dd, yy-mm-dd, yyyy/mm/dd, and yy/mm/dd) va R =/^ (\ d {2} | \ d {4 }) [\/-] \ d {1, 2} [\/-] \ d {1, 2} $/; if (r. test (myString) {}// --------------------------------------------------------------------------- · Array (Array) 1. declare var arr = new Array (); // declare an empty Array var arr = new Array (10 ); // declare an Array of 10 lengths var arr = new Array ("Alice", "Fred", "Jean "); // use the value to initialize the array var arr = ["Alice", "Fred", "Jean"]; // use the value to initialize the array var arr = [["", "B", "C"] [1, 2, 3]; // declare a two-dimensional array 2. array access arr [0] = "123"; // assign a value to var str = arr [0]; // obtain arr [0] [0] = "123"; // assign a value to a multi-dimensional array 3. conversion between arrays and strings var arr = ["A", "B", "C", "D"]; // declaration // convert the array into a string var str = arr by separator. join ("|"); // Result: "A | B | C | D" // string is cut into an array arr = str. split ("|"); 4. traverse the array for (var I = 0; I <arr. length; I ++) {alert (arr [I]) ;}5. sort var arr = [12, 15, 8, 9]; arr. sort (); // result: 8 9 12 156. array var arr1 = ["A", "B", "C", "D"]; var arr2 = ["1", "2 ", "3", "4"]; // The two arrays of the prize are combined into a new array v Ar arr = arr1.concat (arr2); // result: ["A", "B", "C", "D", "1", "2 ", "3", "4"] // cut an array into two arrays (parameter 1: Start index, parameter 2: cut length) var arr3 = arr. splice (1, 3); // result: arr3: ["B", "C", "D"] arr ["A", "1", "2 ", "3", "4"] // cut an array into two arrays and add the new value var arr4 = arr in the original array. splice (1, 3, "AA"); // result: arr4: ["B", "C", "D"] arr ["A", "AA ", "1", "2", "3", "4"] // ------------------------------------------------------------------- · custom object 1. declaration: function myUser (uid, pwd) {t His. uid = uid; this. pwd = pwd | "000000"; // default value: this. show = showInfo; // method} // The following functions are not custom objects and are methods of custom objects. continue to read the function showInfo () {alert ("username:" + this. uid + ", password:" + this. pwd)} 2. instantiation: var user = new myUser ("user", "123456"); var user = {uid: "user", pwd: "123456"}; 3. obtain and set alert ("username:" + user. uid); // get user. uid = "newuser"; // set user. show (); // call the show () method //----------------------------------------- ---------------------------- · Variable Function Flow Control 1. variable var I = 1; var I = 1, str = "hello"; 2. function funName () {// do something .} function funName (param1 [, paramX]) {// do something .} 3. nested functions // in some cases, you need to create a function that is unique to the function itself. function myFunction () {// do something. privateFunction (); function privateFunction () {// do something .}} 4. anonymous function var tmp = function () {alert ("only test. ");} tmp (); 5. delay function call var tId = setTime Out ("myFun ()", 1000); // call the myFun () function fucntion myFun () {// do something clearTimeout (tId) after a delay of 1000 milliseconds ); // destroy object} 6. process control if (condition) {} else {} switch (expression) {case valA: statement; break; case valB: statement; break; default: statement; break;} 7. exception capture try {expression} catch (e) {} finally {} // do not handle any exception window. onerror = doNothi Ng; function doNothing () {return true;} // attribute available for the exception class description: exception description (IE, NN) fileName: exception page URI (NN) lineNumber: exception row (NN) message: exception description (IE, NN) name: Error Type (IE, NN) number: Error Code (IE) // error message (compatible with all browsers) try {} catch (e) {var msg = (e. message )? E. message: e. description; alert (msg);} 8. speed up script execution-avoid using eval () function-avoid using the with keyword-streamline the assignment of repeated expressions to the minimum-use indexes in large objects to find Arrays-reduce document. write () usage // --------------------------------------------------------------------------- · browser features (navigator) 1. browser name // IE: "Microsoft Internet Explorer" // NS: "Netscape" var browserName = navigator. appName; 2. browser version bar browserVersion = navigator. appVersion; 3. client operating system var isW In = (navigator. userAgent. indexOf ("Win ")! =-1); var isMac = (navigator. userAgent. indexOf ("Mac ")! =-1); var isUnix = (navigator. userAgent. indexOf ("X11 ")! =-1); 4. determines whether an object, method, attribute, // when an object, method, or attribute is not defined, undefined or null is returned. These special values are false if (document. images) {} if (document. getElementById) {} 5. check if (navigator. userLanguage) {var l = navigator. userLanguage. toUpperCase ();} 6. check whether the browser supports Cookies if (navigator. cookieEnabled) {}// --------------------------------------------------------------------------- · control the browser window (window) 1. set the browser size window. resizeTo (800,600 ); // Adjust the browser size to X window. resizeBy (50,-10); // increase or decrease the size of the window in the original size. 2. adjust the position of the browser window. moveTo (10, 20); // positions the browser to X: 10 Y: 20 window. moveBy (0, 10); // move the position (offset) on the original position 3. create a new window var win = window. open ("about.htm", "winName", "height = 300, width = 400"); // The alwaysLowered parameter is always behind other browser windows (NN) alwaysRaised // always in front of other browser windows (NN) channelMode // whether it is in navigation mode (IE) copyhistory // copy the history to the new window (NN) dependent // close a new window when its main window is opened (NN) Fullscreen // full screen mode (none of the related toolbar) (IE) location // whether to display the address bar (NN, IE) menubar // whether to display the menu bar (NN, IE) scrollbars // whether to display the scroll bar (NN, IE) status // whether to display the status bar (NN, IE) toolbar // whether to display the toolbar (NN, IE) directories // whether to display the link bar (NN, IE) titlebar // whether to display the title bar (NN) hotkeys // display the menu shortcut key (NN) innerHeight // The height of the content area (NN) innerWidth // content area width (NN) resizable // whether the size can be adjusted (NN, IE) top // the size of the window from the desktop border (NN, IE) left // the size of the window from the left border of the desktop (NN, IE) height // The window height (NN, IE) width // browse Device width 4. communicate with new window win. focus (); // get the focus win.doc ument for the new window. write ("abc"); // operate win.doc ument in the new window. close (); // end the stream operation opener. close (); 5. mode window. showModalDialog ("test.htm", dialogArgs, "param"); // pass the window object. showModelessDialog ("test.htm", myFunction, "param"); // pass the function window. dialogArguments // dialog box access the Object window passed by the parent window. returnValue // value returned by the "obtain parent window" dialog box // parameter center // dialogHeight of the window center // dialogWidth of the window height // dialogTop of the window width // window Distance from the port to the top margin of the screen: dialogLeft // distance from the window to the left of the screen to the edge // border style (raised | sunken) help // display the help button resizable // whether the window size can be changed status // whether the status bar is displayed // example <script> function openDialog (myForm) {var result = window. showModalDialog ("new.html", myForm, "center ");} </script> <form action = "#" onsubmit = "return false"> <input type = "text" id = "txtId"> <input type = "button" id = ""btnChk" value = "verify whether it is available" onclick = "openDialog (this. form); "> </form> // another New.html <script> window. dialogArguments. btnChk. enabled = false; // set the button in the parent window to unavailable // do something to check the Id. window. write ("User Id:" + nvidialogarguments.txt Id. value + "usable! "); // Get the value of the text box </script> // ------------------------------------------------------------------- · Management Framework webpage (frames) 1. create a framework architecture webpage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.