JS Basics Review 6 (variables, pre-parsing, referencing, indexing)

Source: Internet
Author: User
Tags try catch

One, variable1. Scope: Global, local, closure 2.   var a=12; Without Var, it becomes a global variable. 3. Global variables: Easy to duplicate name, affect performance 4. Local variables and global variables have the same name, and local blocks are masked globally. 5. Adding something to the window is global. Purpose: To change the contents of a closed space into a global.    (function () {window.a=12; alert (a);}) () second, pre-analysis1. The system will put the declaration of all variables on the top. Just declare it to the top, and the assignment is in its original position. 2. Pre-resolved scopes: Do not detach from the original scope. function, the variable is placed at the top of the function, and globally, on the top of the program. 3. Scope can break through script: read a script---> Pre-parse---> Execute---> Read Next script<script> alert (a);//a is not defined</ script><script> var a = 12;</script><script> alert (a);//a=12</script>4. Start by assigning variables well to avoid the back toss 5. Functions also have pre-parsingif (num%2==0) {alert (' even ');} else{alert (' odd ');} var num=12; pre-parse, Var num, here is undefined, so undefined%2 is undefined, false, so go else example: AAA ();//Error Here AAA is not a functionvar aaa=    function () {alert (1); So, the function assigns a value to the variable and follows the rules of the variable. Iii. References1. Object is not copied when it is assigned, only two variables point to the same piece of memory. Everything is stored in memory. var Arr=[12,4,3];var arr2=arr;arr2.push (6), alert (arr+ ' \ n ' +arr2);//both are [12,4,3,6]2]. If you want to resolve it, add it separately through the for loop. var arr1=[12,5,3];var arr2=[];for (var i=0; i<arr1.length; i++) {Arr2.push (arr1[i]);}                Arr2.push (6);  Arr2=[12,4,3,6];alert (arr1+ ' \ n ' +arr2); arr1=[12,4,3] Iv. Index1. CharAt (Index): Gets the string of a position; 2. string--encode: charCodeAt (index): Gets the encoding of a location string. a->97 b->98 c->99 z->122 =->61?->63 1->49 2->503. encoded--string:string. fromCharCode (coded) For (Var i=1;i<=10000;i++) {document.write (i+ "=" +string.fromcharcode (i) + "<br/>");} 4. Chinese : 4e00~9fa5:19968~40869. js, hex with 0x 4e00--> Print all Chinese characters: for (var i=0x4e00;i<=0x9fa5;i++) {document.write (String.fromCharCode (i) + ""); } 5. Use hexadecimal to denote Chinese characters:Alert (' \u4E00 ')---where "\" is an escape character 6. All strings are encoded and string comparisons are sorted by encoding size Encryption:Obtn1.onclick=function () {var str=otxt1.value;      var str2= "";      for (Var i=0;i<str.length;i++) {Str2+=string.fromcharcode (Str.charcodeat (i) +5); } otxt2.value=str2; } v. Issues relating to comparisons (1) string comparisonis the content, as long as the text is the same, equal a<b a<z z>a (2) array: Belongs toThe object type is new, so the following arr1 and arr2 are not equalvar arr1=[12,5];    var arr2=[12,5];   alert (ARR1==ARR2); False (3) function:function aaa () {alert (1);} function bbb () {alert (1);}  alert (AAA==BBB); false; full notation: new function (); So, if it's new, it's not equal. (4) byte lengthLetter: 1 bytesKanji: 2 or 3 bytes UTF-8 format, 3 bytes gb2312--2 bytes to understand header informationCalculate byte lengthvar str= ' abc Ah ';
function Getbylon (str,type) {
var res=0;
for (Var i=0;i<str.length;i++) {
if (Str.charat (i) >= ' \u4e00 ' &&str.charat (i) <= ' \u9fa5 ') {
if (type== ' Utf-8 ') {
res+=3;
}else{res+=2;}
}else{res++}
}
return res;
}
Alert (Getbylon (str, ' utf-8 '));
vi. in-- detection property exists var json={a:12, B:5};alert (A in JSON);  Error because no quotes--Variable alert (' C ' in JSON); False Note: The string must be used only for JSON, not for array compatibility, but what is not used to detect it? var json={a:12, b:5};if (JSON.A) {alert (1);} else{alert (0);} Small problem, 0 is false, if the coincidence Json.a value is 0? --Projectile 0 Seven, abnormal(1) What is an exception: the error that the program cannot handle (2) Try catch: Prevents an error. There is a problem in the try and the catch will execute Try{alert (getComputedStyle (obox,false). width)}//can use it; chrome,ff,ie9++;
catch (E) {//e: information about the error;
Code for Remediation:
alert (e.message);//error details;
Alert (oBox.currentStyle.width)//use it; IE series;
}
The code in the try executes slowly and is not well debugged. So, JS is not much use. For emergencies. Viii. Expressions(1) An assignment expression:alert (a=12); The assignment statement itself also has a value(2) Lian et: alert (a=b=c=5);//5(3) comma expression: Listen to the last one. and cannot be used with Var. the comma has a low priority.   Alert ((1,3));//Must be added (); if (12,0) {alert (1);} else{alert (0);}

JS Basics Review 6 (variables, pre-parsing, referencing, indexing)

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.