JS getting acquainted

Source: Internet
Author: User
Tags float double naming convention

JS is an object-oriented language

    1. Packaging
    2. Inherited
    3. Polymorphic
    4. Clustered (has the ability to reference other objects in the object)

The majority of situations in JS use do not require object-oriented design, in many cases the use of objects that have been designed and prepared, based on the language of the object.

Use of JS (introduced)

    1. Write directly in the script tag in JSP, HTML
    2. Introducing in the script tag

  Attention:

      1. The script tag location is arbitrary. But the higher the position, the earlier the execution. ( important )
      2. JS External File The recommended extension is. js. But in fact any suffix name can be.
      3. Using the script tag to introduce an external file, you should not write the JS code in the script tag.

The basic syntax of JS

    • Variable declaration

Variable declarations use Var, where variables are case-sensitive STR and STR are not the same variables. Strings can be wrapped using double quotation marks ("") or single quotation marks (""). The type of the variable can be changed at any time. The naming convention is the Hungarian notation.

var str = 1;  var str = "Hello"; There's no difference between the two.

    • End of line uses ";" as the ending symbol for a line. (There can be no ";", with a line break (carriage return) as the end of a line.) (Not recommended)
    • There are two types of annotations in JS, multiple lines

Single-line Comment "//" Multiline comment "/* */"

    • Encapsulate the code block like Java, using {}.
    • Variable declaration, the prefix VAR is also not required.
      What is the difference between adding Var and not adding Var? If Var is not used, then the variable is a global variable

Type of JS

JS is divided into two types of

      1. Raw data type
      2. Object data type

Basic data types in Java have a byte short int long float double Boolean char
JS in the original data type has number (numeric, floating point type, integer type)
String (JS language breakthrough, no char type.)
Boolean
Null (used to indicate a placeholder for the reference data type.) is usually an artificial assignment. var person = null;
Undefined (the value derived from NULL is when we declare a variable,)
1. If the variable is not initialized, the system will default to the value undefined
2. There is no return value in the function, then the default return undefined
Primitive data Type qualifier:
typeof is used to determine which primitive type a variable is.
Null returns object, which is a bug in JS, which is considered appropriate. So the bug was preserved.

The statement in JS

There is a loop in Java: for while-do Do-while
There is a judgment: if switch
The statement in JS is exactly the same as in Java!

Special: The enhanced for loop in Java is not in JS.
JS has a for (Var xx in xx) statement. Used to traverse the properties of an object.

JS in operator

  Unary operators
unary addition, subtraction
var a = +1;
var B =-1;
Advanced applications in JS.
var c = + "1"; Making type conversions
var d = + "abc"; Conversion failed, returning the special value NaN in number.

Boolean operator! && | |
Automatic type conversion in JS. Transformation Law (emphasis)
String ==> "" is converted to false and the others are true;
Number ==> except for nan,+0 and-0. All others are converted to true.
Null ==> False
Undefined ==> false
Nan Features:
Any Boolean operation that Nan participates in returns a value of false. except = =, because undefined is null derived, so alert (undefined = = null);//True
Comparison operators

alert (11>3); True
When an operator ends with a number, and one end is another, the other type is automatically converted to the number type
Alert ("one" >3); True
Alert (11> "3"); True
When a string is compared, the rule is: Compare the first character ASC code. If same, compare 2nd bit ...
Alert ("One" > "3"); False
Alert ("One" > "1"); True
Alert ("abc" >11); False

Equal sex operator = = = = = = = = = =
All equals: = = = The type is included when comparing.

Objects in the ECMAScript

    1. Object is a superclass of all objects.
    2. function objects and the creation of function
      1. var fun1 = new Function ("A", "B", "Alert (a+b);");
      2. var fun2 = function (A, b) {alert (a+b);}
      3. function Fun3 (A, b) {alert (A+B)}
    3. Call to function
    4. The invocation of a function in JS only looks at the function name. Built-in object arguments when called, arguments represents the actual argument list during function run.
      Arguments.length Number of actual parameters
      ARGUMENTS[0] the first parameter.
      Application: Arguments implements overloading of functions.
    5. Return of function
      1. If the function does not display the specified return value, then the function return value is undefined.
      2. Use the return keyword to return content
      3. The return keyword, in JS can also be used as the end method to run the function.
      4. Application of the void operator.

The classification of objects in ECMAScript

    1. Local objects
      Built-in objects do not need to create an instance. Use Global Math directly
      Host Object DOM BOM two parts.
      3 wrapper objects in ECMAScript.
      String number Boolean
      Pseudo-Object: String Number Boolean These 3 primitive types can be thought of as pseudo-objects and can invoke the methods and properties of the wrapper object directly.
      String object property Length
      Important method
      IndexOf
      LastIndexOf
      CharAt
      Alert (Str1.charat (0)); A
      charCodeAt returns the ASC code for the character that is located
      Alert (str1.charcodeat (0)); 97
      SubString
      Alert (str1.substring (0, 1)); A
      Slice supports negative numbers. Go from right to left.
      Alert (Str1.slice (0,-1)); A
      The method of combining with the regular
      Split
       
      Replace
      Match
      Search

Global Object

JS getting acquainted

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.