JavaScript notes 1

Source: Internet
Author: User
Tags arithmetic operators logical operators

JavaScript is a literal-translation scripting language, which is a dynamic type, a weak type, a prototype-based language, and a built-in support type. Its interpreter, known as the JavaScript engine, is widely used as a scripting language for the client, and is used in HTML (an application under the standard Universal Markup Language) to add dynamic functionality to an HTML Web page.
JavaScript programmers are usually referred to as JS;
Characteristics:
1: A scripting language that relies on HTML to run;
2: Do not need to compile, directly explained by the browser run;
3: Weakly typed language, when defining variables, do not need to specify an explicit data type, the data type of the variable is completely determined by the value;
4:javascript is Object-based and has a lot of built-in objects;

5.JavaScript strictly case-sensitive, if the case is wrong, the program will error or not run properly

Two. Composition
There are 3 main components: Ecmascript/dom/bom
1.ECMAScript describes the syntax and basic objects of the language.
2. Document Object Model (DOM), which describes methods and interfaces for handling Web page content.
3. The browser object model (BOM), which describes the methods and interfaces that interact with the browser.

Three. Data type
Basic data type:
Undefined: Represents a non-existent variable;
Number: numeric type, including integers and decimals;
String: Strings;
Boolean:true or false;
Reference data type:
Null: Represents the default value for the reference data type;
Object

Four. Supported operators
Arithmetic operators:


Assignment operators:


Comparison operators:


Logical operators:

Five. Basic syntax
:
     1: Write JS file separately;
     2: Use the <script></script> tag anywhere in the HTML page to write JavaScript code inside the tag;
     3: In the start tag, you can also write a line of JavaScript code directly;
Note: The format of single-line comments (//) and multiline annotations (/*  */) is consistent with Java;
Function:
     use keyword function:
     format:
          function Method Name (parameter list) {
         code
         
     Note: 1. The parameter list is only in the form of a variable name, no data type;
     2. When a call is made, any number of arguments can be passed, regardless of the method definition, and the subsequent value is lost if the parameter is more than the defined parameter;
Define variables:
     var variable name = data value; (var fixed!!)
Basic statement:
     alert (variable or data value);  Popup display value
     Typeof (variable or data value);   Determine the data type of a variable or data value;

Six. Data type conversion
Other data types---->boolean:
1. Not 0 is true, 0 is false;
2. The string length is 0 false, greater than 0 is true;
3. The existence of the object is true and does not exist as false;
The 4.null/undefined are all false;
String--->number:
Parseint method;
Special cases of equal sex comparisons:
1.NaN = = NaN, returns FALSE.
2.2== true to return Fasle. Other data types will be shifted to number type and then compared;

Seven. JS gets the tag object in HTML
document.getElementById ("id attribute value");(d Ocument is the current HTML page).
For this label object, you can use the label's property name to get the corresponding property value; Common properties are:
Value: Gets the values in the input box
InnerHTML: Gets the element body of the tag;
Example: (The body part of the following HTML, three picture carousel effect)

  1 <body>  2        3      <script type= " Text/javascript ">  4      //Set Timer Jswindow method  5     setinterval (  6          switch1);  7      //define switch function  8     var i = 1;  9      function Switch1 () {//switch is a keyword and is not available with the ten         var pic = document.getElementById ("img");//You can use any of the variable names for the IMG tag you have taken ( pic) receives one         PIC.SRC = "img/" + (++i) + ". jpg";          if (i = = 3) {i              = 0; +          } +      </sc Ript>  </body>

Eight. SetInterval () (Bom-window)
Grammar:
SetInterval (CODE,MILLISEC)
Invokes a function or evaluates an expression by the specified period (in milliseconds).
Example: (see Seven)


Nine. SetTimeout () (Bom-window)
Grammar:
var t=settimeout ("javascript statement", MS):
The first parameter is a string that contains a JavaScript statement. This statement may be such as "alert (' 5 seconds! ')" or a call to a function, such as alertmsg () ".
The second parameter indicates how many milliseconds from the current start to execute the first parameter.
The function closes automatically when it finishes executing.
Example: (Effect: Two seconds to display the ad image)

  1 <body>  2      <div style= "Display:none" id= "IMGGG" >  3            4      </div>  5      <div>  6            7      </div>  8      <script type= "Text/javascript" >  9      //Set Timer     SetTimeout (show, +);      function Show () {          //Get the image of the ad div.          var div = document.getElementById ("IMGGG");          div.style.display = "block";          setTimeout (hide, +); + +     function Hide () {19          var div = document.getElementById ("Imggg"),          Div.style.display = "None";          SetTimeout (Show, 2000);      </script> 24  

JavaScript notes 1

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.