strangedeのブログ -25ページ目

strangedeのブログ

ブログの説明を入力します。

JavaScript contacting more than two years experienced a variety of errors, some of which people are very hard, the original on JavaScript misunderstanding so deep, only this article summarize the common variety granted misunderstanding. String replacestring replace method we often use to replace some of the characters in string, like this, like grammar string.replace (subStr / reg, replaceStr / function) The first argument is the string you want to find, or a regular expression, the first Two parameter is a string or a way want to make into, we can so use \u0026 quot; I'm Byron \u0026 quot; .replace (\u0026 quot; B \u0026 quot;, \u0026 quot; b \u0026 quot;) // I'm byron demerit and we would like to have the same but \u0026 quot; I'm a student, and you \u0026 quot;? .replace (\u0026 quot; n \u0026 quot;, \u0026 quot; N \u0026 quot;); // I'm a studeNt, and you and we expect is not the same, and the second? 'n' has not been replaced. Replace method of the string if the first argument to a string, then only the first match will be replaced. If you want to replace all matches, you need to pass a RegExp object and specify its global properties. \u0026 Quot; I'm a student, and you \u0026 quot;? .replace (/ N / g, \u0026 quot; N \u0026 quot;);? // I'm a studeNt, aNd you so that we can achieve our aim, on string replace method in detail You can look at the use of JavaScript string of replaceDate objects we can construct a Date object new Date () // Date {Fri Aug 02 2013 16:50:33 GMT + 0800 (China Standard Time)} new Date (milliseconds) // Date 579765-700 Sonic Yellow / Sail - Cool Grey - Tour Yellow Nike Air Max LeBron 10 Low Sonic Yellow Online {Fri Aug 02 2013 16:53:26 GMT + 0800 (China Standard Time)} new Date (\u0026 quot; 2013/08/02 \u0026 quot;) // Date {Fri Aug 02 2013 00:00:00 GMT + 0800 (China Standard Time)} new Date (year, month, day, hours, minutes, seconds, ms) before without any problems in three ways, but the fourth round and we expect the results obtained are inconsistent new Date (2013,08,02) / / Date {Mon Sep 02 2013 00:00:00 GMT + 0800 (China Standard Time)} We can see that the incoming month is 08, the result returned is September. This is because the month the Date object is counting from 0 (the day is not), that is 0 for January, 1 for February ... 11 representatives of 12 months. Date instance when calling getMonth method with particular attention var d = new Date (2012, 4, 15); // 2012 年 5 月 15 日 alert (d.getMonth ()); // result is 4Date.parseDate.parse The method can identify two formats string argument (standard long date format, such as with the kind of week, it can be recognized but not used): 1 'M / d / yyyy':. American date display format. If passed two years the process as 19xx 2. 'yyyy-MM-dd' or 'yyyy / MM / dd': Note the month and day must be two Date.parse returns the result is not a Date object, but From 1970-01-01 midnight (GMT) to give the number of milliseconds between the given date. You can use the constructor Date will convert it to a Date Mens Nike Free 3.0 Wool Skin Shoes Grey Yellow object. new Date (Date.parse (\u0026 quot; 8/2/2012 \u0026 quot;)); // correct identification of 2 August 2012 new Date (Date.parse (\u0026 quot; 2012-08-02 \u0026 quot;)); // correct identified as 2 August 2012 new Date (Date.parse (\u0026 quot; 2012-8-2 \u0026 quot;)); // does not recognize for ... in loop through the array for ... in one object to iterate members (properties, methods ), the results if used to iterate to the values ​​for each array 579765-700 Sonic Yellow / Sail - Cool Grey - Tour Yellow Nike Air Max LeBron 10 Low Sonic Yellow Online is not expected, the method can be traversed God horse out Array.prototype.contains = function (item) {for (var i = 0; i \u0026 Air Max 2011 Womens Grey Red Black lt; = this.length - 1; i ++) {if (this [i] == item) return this [i];}} var staff = [\u0026 quot; Staff A \u0026 quot ;, \u0026 quot; Staff B \u0026 quot;]; // Normal Enumeration: Only the 2 items are enumeratedfor (var i = 0; i \u0026 lt; = Nike Free 3.0 V4 staff.length - 1; i ++) {var singleStaff = staff [i]; alert (singleStaff);} // for ... in Enumeration: the method \u0026 quot; contains \u0026 quot; are enumerated, toofor (var singleStaff in staff) {alert 579765-700 Sonic Yellow / Sail - Cool Grey - Tour Yellow Nike Air Max LeBron 10 Low Sonic Yellow Online (singleStaff);} In fact, many times we will add other attributes to the array. Such as jQuery object is an array of objects with some extension methods; another example NIKE KD 6 NSW String.prototype.match method returns a value is an array (regular expression matches its sub-expressions) plus index and input two properties. parseInt Syntax: parseInt (string, radix) Parameter Description string Required. The string to be parsed. radix Optional. He pledged to resolve the digital base. The value is between 2 to 36. If you omit this parameter or the value is 0, then the number will be 10 basis resolution. If it is '0x' at the Air Max 2011 Womens Grey Red Black beginning or '0X' to, it will base 16. If this parameter is less than 2 or greater than 36, the parseInt () returns NaN. When parameter radix is ​​0 or not set the parameters, parseInt () will be judged according to string numerical base. For example, if the string (at the end of the beginning of the space 2015 Nike Free 5.0 is automatically omitted) with '0x' at the beginning, parseInt () the rest will be resolved to a hexadecimal string of integers. If the string 0 at the beginning, then the ECMAScript v3 allows parseInt () followed by an implementation of the resolve of characters as octal or hexadecimal numbers. If the string begins with a number from 1 to 9 to, parseInt () will resolve it to a Air Jordan Outlet decimal integer. If the first character can not be converted Air Max 2011 Womens Grey Purple to digital, then parseFloat () returns NaN. parseInt (\u0026 quot; 10 \u0026 quot;); // returns 10parseInt (\u0026 quot; 19 \u0026 quot;, 10); // returns 19 (10 + 9) parseInt (\u0026 quot; 11 \u0026 quot;, 2); // returns 3 (2 + 1) parseInt (\u0026 quot; 17 \u0026 quot;, 8); // returns 15 (8 + 7) parseInt (\u0026 quot; 1f \u0026 quot;, 16); // returns 31 (16 + 15) parseInt (\u0026 quot; 010 \u0026 quot;); // undecided: Return 10 or 8setTimeout / setInterval execution timing setTimeout () and setInterval () it is often used to deal with the delay and timing tasks. setTimeout () method is used to evaluate the expression or function call after the specified number of milliseconds, while setInterval () you can call the function or expression circulating in every specified number of milliseconds until clearInterval to clear it. JavaScript is actually running in a single-threaded environment, which means that the timer is only planned a future time code execution, and specific execution timing is not guaranteed, because the life cycle of the page, there may be at different times other JavaScript code control process. Run after page to download the complete code, event handlers, Ajax callbacks are using the same thread, in fact, responsible for ordering the browser, is assigned a certain program priorities at a point in time to run. We can imagine to run JavaScript timeline. When the page is loaded when the first implementation of the use of the back page lifecycle method and variable declarations and data processing, after which the process will wait for more JavaScript code execution. When the process is idle, the next piece of code would be triggered in addition to the main JavaScript process, we also need the code execution queue one at a time in idle process. With the page life cycle goes on, the code will be added to the queue in accordance with the order of execution, such as when a button is pressed when he event handler will be added to the queue, and the next time a possible execution. Upon receipt of an Ajax response, the code callback function will be added to the queue. JavaScript does not have any code that is executed immediately, but once the process is performed as soon as possible idle. Timer queue works is when a certain time after the code into the past, this does not mean it will be immediately executed, can only express it performs quickly. About setTimeout / setInterval execution timing detail can look setTimeout () and setInterval () when it is called to perform pre-analysis console.log (a); // Error: a is not defined, a direct error, the following statement is not executed, Results for the comment about the sentence Results console.log (b) // undefinedvar b = \u0026 quot; Test \u0026 quot ;; console.log (b); // Test before two very strange variables a, b are not declared, first sentence but being given, the second sentence can be output undefined? This is because JavaScript is an interpreted language, but it is not a direct progressive implementation, JavaScript parsing process is divided into two successive stages, a pretreatment stage, the other one is the implementation phase. JavaScript interpreter will complete the JavaScript scripting code into byte code, and then the second phase of the JavaScript interpreter execution environment by means of the bytecode generation machine code, and the execution order in the pretreatment stage. Before (Source Nike Air Max undefined results in the implementation of the first sentence of the statement; it says the value of the implementation of the first sentence before the JavaScript statement has been the function / variable declaration pretreated, var b = 'Test' is equivalent to two statements, var b already resolved), b = 'Test' (the phrase is executed sequentially performed after the second sentence). This is why we can before the method declaration statement on the reasons for calling the method. showMsg (); // This is messagefunction showMsg () {alert ('This is message');} block-level scoping JavaScript is not block-level scope, and only function-level scope, which means that only in JavaScript {} You can play the role of grammar blocks, but can not play a role in the scope of the block if (true) {// syntax block, to ensure that the code within {} if condition is true execute // ... var a = 3;} console.log ( a); // 3 above example can clearly see console.log method belongs window seemingly still can access the local variables of a closure from a classic mistake first talked about, there are a number of div NIKE KD 6 NSW on the page, we want to give them tie Given an onclick method, so with the following code \u0026 lt; div id = \u0026 quot; divTest \u0026 quot; \u0026 gt; \u0026 lt; span \u0026 gt; 0 \u0026 lt; / span \u0026 gt; \u0026 lt; span \u0026 gt; 1 \u0026 lt; / span \u0026 gt; \u0026 lt; span \u0026 gt; 2 \u0026 lt; / span \u0026 gt; Nike KD 7 \u0026 lt ; span \u0026 gt; 3 \u0026 lt; / span \u0026 gt; \u0026 lt; / div \u0026 gt; \u0026 lt; div id = \u0026 quot; divTest2 \u0026 quot; \u0026 gt; \u0026 lt; span \u0026 gt; 0 \u0026 lt; / span \u0026 gt; \u0026 lt; span \u0026 gt; 1 \u0026 lt; / span \u0026 gt; \u0026 lt; span \u0026 gt; 2 \u0026 lt; / span \u0026 gt ; \u0026 lt; span \u0026 gt; 3 \u0026 lt; / span \u0026 gt; \u0026 lt; / div \u0026 gt; $ (document) .ready (function () {var spans = $ (\u0026 quot; #divTest span \u0026 quot;); for (var i = 0; i \u0026 lt; spans .length; i ++) {spans [i] .onclick = function () {alert (i);}}}); a very simple function but has chosen the wrong one, the value of each alert are 4 simple modification enough Air Max 2011 Womens Blue Grey to make the var spans2 = $ (\u0026 quot; # divTest2 span \u0026 quot;); $ (document) .ready (function () {for (var i = 0; i \u0026 lt; spans2.length; i ++) {(function (num) {spans2 [i] .onclick = function () {alert (num);}}) (i);}}); closure means have access to another function scope variables, creation of common closure way is to create an internal function to another function, as long as there may be calls an internal function, JavaScript will need to retain the function is referenced. And the need to track all the variables cited this internal function of the JavaScript runtime until the last variable obsolete, JavaScript garbage collector to release the corresponding memory space. This book is very superficial, like a simple understanding of JavaScript closures can look at what is the end closures of potholes on JavaScript plenty of money, while learning side of the summary of it. Some common errors ofJavaScript