JavaScript Intermediate Grammar Quick Start _javascript tips

Source: Internet
Author: User
Tags modifiers

1. Talk about the object of JavaScript

JavaScript is a language of a weak language type and a dynamically typed language. In the process of using JavaScript, it is often necessary to use the built-in objects of JavaScript as well as the custom objects.

1.1 How to create an object
JavaScript is a weak language type language, do not have to be like Java, C # and other high-level languages to create objects through the constructor method, in JavaScript, there are two main ways to create objects, one is directly through the New keyword definition, one is defined by the function. As follows:

  The first way is to create an object directly from new;
  var demo = new Object ();
  Demo.name = "Anderson";
  Demo.sex = "male";
  Demo.age = n;

  The second format, which is directly instantiated, is basically consistent with the JSON syntax and is generally used as an alternative syntax for the first method.
  var demo = {name: "Anderson", Sex: "Male", age:25};

  The third format, through the constructor, creates object
  function Demo (name,sex,age) {
    this.name = name;
    This.sex = sex;
    This.age = age;
  }
  var demo = new Demo ("Andeson", "male", 25);

1.2 How to modify an object

JavaScript and high-level language object syntax is not the same, in general, the limit is relatively small, when the object is created, you can dynamically modify the object's properties, methods, such as adding a new attribute, add a new method, and so on.

  var demo = {Name: "Anderson"};
  demo. Sex = "male";

1.3 Understanding Digital objects in JavaScript

In JavaScript, all numbers are 64 bits, and all numbers are made up of floating-point types. JavaScript uses the IEEE754 standard-defined 64-bit floating-point format to represent numbers that represent a maximum of ±1.7976931348623157 x 10308, and a minimum value of ±5 x 10-324.

  var demo1 = 7;    Decimal notation
  var demo2 = modified;   Octal notation
  var demo3 = 0x12;  Hexadecimal notation

  var Demo4 = 12e12;  The scientific notation notation
  var demo5 = 12e-12;//scientific notation notation

  var demo7 = 128;
  var Demo8 = demo7.tostring (16); Converts to hexadecimal
  var demo9 = demo7.tostring (8);  Convert to octal
  var demo10= demo7.tostring (2);  Convert into binary

  var demo11= Infinity;   Infinitely large notation
  var demo12= isnan (m);  Non-numeric value notation

  var demo13= typeof (a);       The number is of type
  demo14= typeof (New), and the//number type is Object
  var demo15 = (Demo13 = = demo14);   Both equal
  var demo16 = (demo13=== demo14);   The two are not equal

1.4 Understanding String Objects in JavaScript

  var str = "Hello,anderson";

  var demo1 = str[0];        Gets the characters in the string by index, and returns undefined
  var demo2 = str.indexof ("H") if the maximum value is exceeded;   Gets the position of the target string in the source string, and returns-1
  var demo3 = str.length;      Gets the length of the string
  var Demo4 = str.lastindexof ("H");//Gets the position of the destination string in the source string, starting at the end of the string, and cannot find the return-1
  var demo5 = Str.match (" Anderson ");//Match the target string from the source string, if so, return the target string, otherwise, return null
  var demo8 = str.replace (" A "," D ");//Replace the target string in the source string with the replacement string
  var demo9 = Str.touppercase ();  Convert to uppercase
  var demo10= str.tolowercase ();  Convert to lowercase
  var demo11= str.split ("D");    Convert an array of

  var demo12= "\";    Represents the single quote
  var demo13= "\";    Represents a double quote
  var demo14= "\";    Represents a slash
  var demo15= "\ n";    Represents the newline
  var demo16= "\ r";    Represents a carriage return
  var demo17= "\ t";    Represents a tab character
  var demo18= "\b";    Represents
  the space var demo19= "\f";    Represents a page change

1.5 Understanding Date objects in JavaScript

  var demo1 = new Date ();      Create object, current date
  var demo2 = new Date (22E9);    Create object, number of milliseconds
  var Demo3 = new Date ("2016-06-04");//Create object, date string
  var Demo4 = new Date (2016,5,12,12,12,12);//Create Object,

  Demo5 = (Demo1 > Demo2);    Compare date

1.6 Understanding the array objects in JavaScript

  var demo1 = new Array (1,2,3,4);    Create Array
  var demo2 = [1,3,4,5];        Create Array
  var Demo3 = Demo1.concat (DEMO2);   Merge Array
  var demo5 = Demo1.concat (DEMO2,DEMO3);//merge array

1.7 Understanding regular objects in JavaScript regexp

REGEXP is the abbreviation for regular expressions (Regular Expression). Regular expressions are primarily used for retrieval of text. Its basic grammatical form is as follows:

  var pattern = new RegExp (pattern,modifiers); Create a regular object by constructing the method
  var pattern =/pattern/modifiers;       A direct declaration of a regular object

  //Where there are two modifiers modifiers, I and g,i represent case-insensitive, and G represents the Full-text search
  //pattern represents the retrieval model

  var str = "Hello, Anderson Lu";
  var pattern =/llo/gi;
  var demo1 = Str.match (pattern); Use instance

  var pattern2 = new RegExp ("\\Lu\\gi");
  var demo2 = pattern2.test (str); To determine if there are strings in Str that match the pattern pattern2, return True or False
  var Demo3 = pattern2.exec (str);//return matching string

Well, this is the end of the article, and there are other objects, such as math arithmetic and Boolean, and so on. You can learn through the cloud-dwelling community.

Related Article

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.