Front-end JavaScript classic face question

Source: Internet
Author: User
Tags try catch sessionstorage

1.alert (1&&2), Alert (1| | 0)

The result of alert (1&&2) is 2.

As long as "&&" is preceded by false, whether "&&" is followed by true or false, the result will be returned to the value preceding "&&";

As long as "&&" is preceded by true, whether "&&" is followed by true or false, the result will be returned to the value after "&&";

This is just the right thing to do, and there has to be a return value. This is mainly because only the preceding is true, the subsequent operations are performed, and the values returned are the ones that follow. If the preceding is false, then there is no sense in the back, directly returning the previous result.

Alert (0| | 1) The result is 1

As long as "| |" False in front, regardless of "| |" Followed by true or FALSE, return "| |" The value that follows.

As long as "| |" Preceded by true, regardless of "| |" Followed by true or FALSE, return "| |" The preceding value.

This is the right thing to do, but there must be a return value, preceded by true, to return the previous value directly, if the preceding is False, returns the following value.

The difference between 2.mouseenter and mouseover

The MouseOver event is triggered regardless of whether the mouse pointer passes through the selected element or its child elements. Correspondence Mouseout

The MouseEnter event is triggered only when the mouse pointer passes through the selected element. Correspondence MouseLeave

As a result, a common bubbling phenomenon occurs when you define the mouse position using MouseOver.

Ways to block Bubbles

Audience: Event object. Stoppropogation (),

IE: Event object. cancelbubble=true;

3. Regular expressions

Matches a string with a regular expression, beginning with a letter, followed by a number, string, or underscore, with a length of 9-20

var reg=new RegExp ("^[a-za-z][a-za-z0-9_]{9,20}$");

Regular expression Analysis ID number

ID Number = 6-bit address code + 8-bit Birth date + 4-bit sequential number

For example: The birth date part of the person's ID number between 1800-2099

(18|19|20) {0,1}\d{2} (0[1-9]|1[012]) (0[1-9]|[ 12]\D|3[01]);        (18|19|20) {0,1}\d{2}  determines the year,        (18|19|20) {0,1}       determines the first two bits of the year  (18|19|20) match, {0,1} Represents a match for the previous item at least 0 times        \d{2} The                 last two bits of the year are determined to matchany number in [0-9], matching two times                (0[1-9]|1[012])       Determine the month        ,0[1-9]                match January-September, the first is 0, in 1-        9 Choose a 1[012]                match October-December, the first is 1, in [012] Choose one                 0[1-9]| [12]\d|3[01]   determine date         0[1-9]                OK 01th-09th        [12]\d                OK 10th-29th        3[01]                 OK 30th-31st No.

Regular expressions cannot contain special characters

var reg=new RegExp ("^[\u4e00-\u9fa5a-za-z0-9_]{1,20}$");

Reg.test ("string to detect");

Test is a method of Reg that matches the condition to true, and does not match the condition return false

4. Data storage

Several ways to store data

The cookie stores the data in the client and sends it to the server, which is used to store, get, delete, or expire when the browser closes, but only the string type is stored in the form of text.

Localstorage: Stored locally will not be sent to the server, no time limit, unless manually deleted, but only the new browser is supported.

Session through the data structure like a hash table to store, can support any type of data objects, can contain multiple objects, no size control, high security, save in the server, with the increase in storage, for users more sites, the server pressure will become larger.

Access to Cookies

Save:

       var New Date ();         Expiredate.setyear (Expiredate.getyear ()+1900+1);         $cookies. Put (' account ', data.user,{' expires ': Expiredate.toutcstring ()});

Take:

$scope. user = $cookies. Get (' account ');

Access to Localstorage

Save:

Localstorage.setitem ("Name", "star");

Take:

     var username =  Localstorage.getitem ("name");


Access to session

Save:

Sessionstorage.setitem ("CardId", Keycardid);

Take:

var cardId = Sessionstorage.getitem ("CardId");

5. Related scopes and exception captures

Analyze the output of the following code

1.

if (true) {   var a = 1;} Cosole.log (a);

This print is 1 because the definition variable is equivalent to a large function when there is no function name, and the variable defined in the if is equivalent to a global variable.

2.

function aaa () {  var a = 1;  Console.log (a);}console.log (a); AAA ();

This situation prints ' a undefined ', and the function aaa () is no longer executed because the error above will affect the execution of the following function.

3, how to let the error does not affect it, we can use try Catch

function aaa () {var a = 1; Console.log (a);} Try {   console.log (a);} Catch (e) {}aaa ();

Use a try catch to wrap the Console.log (a) that may be wrong, so that the execution of the subsequent function is not affected by the time the problem occurs.
4, see how Trycatch is printed out

Try {   console.log (a);} Catch (e) {    console.log (e);}

This allows you to see exactly where the problem is and does not affect the execution of subsequent functions. The printing results are as follows:

[Object Error] {Description: "" A "undefined", Message: "" A "undefined", "Name:" Referenceer ... ", Number: -2146823279, stack:" Referenceer ... "}

Front-end JavaScript classic face question

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.