Javascript Intermediate Grammar Quick Start
- 2021-07-06 10:01:32
- OfStack
1. Talk about the object of Javascript
Javascript is not only a weak language type, but also a dynamic language type. In the process of using Javascript, it is often necessary to use the built-in objects and custom objects of Javascript.
1.1 How to Create an Object
Javascript is a weak language type language, so it is not necessary to create objects by constructor like Java, C # and other high-level languages. In Javascript, there are mainly two methods to create objects, one is defined directly by new keywords, and the other is defined by functions. As follows:
// No. 1 1 In a way, directly through new Create an object;
var demo = new Object();
demo.name = "Anderson";
demo.sex = "male";
demo.age = 23;
// No. 1 2 Format, direct instantiation , Basically and Json The grammar is 1 As a result, 1 General as the first 1 Alternative grammar in a variety of ways.
var demo = {name:"Anderson",sex:"male",age:25};
// No. 1 3 Format, create objects through constructors
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 Objects
The object syntax of Javascript and high-level language is different. Generally speaking, its limitation is less. After creating an object, you can dynamically modify the attributes and methods of the object, such as adding a new attribute and adding a new method.
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 composed of floating-point types. Javascript uses the 64-bit floating-point format defined by IEEE 754 standard to represent numbers, which can represent a maximum value of 1.7976931348623157 x 10308 and a minimum value of 5 x 10-324.
var demo1 = 7; //10 Binary notation
var demo2 = 07; //8 Binary notation
var demo3 = 0x12; //106 Binary notation
var demo4 = 12e12; // Scientific notation
var demo5 = 12e-12; // Scientific notation
var demo7 = 128;
var demo8 = demo7.toString(16); // Convert to 106 Binary system
var demo9 = demo7.toString(8); // Convert to 8 Binary system
var demo10= demo7.toString(2); // Convert to 2 Binary system
var demo11= Infinity; // Infinity representation
var demo12= isNaN(100); // Non-numerical value representation
var demo13= typeof(12); // The type of the number is Number
var demo14= typeof(new Number(12)); //Number The type of is Object
var demo15 = (demo13 == demo14); // The two are 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]; // Get the characters in the string by index , Returns if the maximum value is exceeded undefined
var demo2 = str.indexOf("H"); // Gets the position of the target string in the source string, and returns if it cannot be found -1
var demo3 = str.length; // Gets the length of the string
var demo4 = str.lastIndexOf("H"); // Gets the position of the target string in the source string, starting from the end of the string, and can't find it. Return -1
var demo5 = str.match("Anderson");// Match the target string from the source string, if yes, return the target string, otherwise, return null
var demo8 = str.replace("A","d"); // Replace the target string in the source string with a replacement string
var demo9 = str.toUpperCase(); // Convert to uppercase
var demo10= str.toLowerCase(); // Convert to lowercase
var demo11= str.split("d"); // Convert to an array
var demo12= "\'"; // Indicates single quotation marks
var demo13= "\""; // Represents double quotation marks
var demo14= "\\"; // Represents a slash
var demo15= "\n"; // Indicates a line break
var demo16= "\r"; // Indicates carriage return
var demo17= "\t"; // Representing tabs
var demo18= "\b"; // Represents a space
var demo19= "\f"; // Represents page change
1.5 Understanding Date Objects in Javascript
var demo1 = new Date(); // Create object, current date
var demo2 = new Date(22e9); // Create object, milliseconds
var demo3 = new Date("2016-06-04");// Create object, date string
var demo4 = new Date(2016,5,12,12,12,12); // Create object, year, month, day, hour, second
var demo5 = (demo1 > demo2); // Comparison date
1.6 Understanding Array Objects in Javascript
var demo1 = new Array(1,2,3,4); // Create an array
var demo2 = [1,3,4,5]; // Create an array
var demo3 = demo1.concat(demo2); // Merge array
var demo5 = demo1.concat(demo2,demo3);// Merge array
1.7 Understanding the regular object RegExp in Javascript
RegExp is the abbreviation of Regular Expression (Regular Expression). Regular expressions are mainly used for text retrieval. Its basic grammatical form is as follows:
var pattern = new RegExp(pattern,modifiers); // Creating regular objects through constructors
var pattern = /pattern/modifiers; // Declare regular objects directly
// Where the modifier modifiers There are two kinds, i And g , i Indicates case insensitivity, g Represents full-text retrieval
//pattern Representation retrieval model
var str = "Hello, Anderson Lu" ;
var pattern = /llo/gi;
var demo1 = str.match(pattern); // Use instances
var pattern2 = new RegExp("\\Lu\\gi");
var demo2 = pattern2.test(str); // Judge str Is there a conformance pattern in pattern2 Returns the string of true Or false
var demo3 = pattern2.exec(str); // Returns a matching string
Ok, this is the end of the article. Besides, there are 1 other objects, such as Math arithmetic and Boolean. You can learn through this site.