JavaScript is an object-oriented language. Everything in JavaScript is an object, but does not use a class, it is based on a prototype (prototype). An object is a special kind of data that has properties and methods.
creating JavaScript objects
Mode 1. Defining and creating an object instance
var person=New Object (); // Create a new instance of the object person.name= "name"; // Add the name attribute person.age=10; // Add Age Property // Another method var person={name: "Name", age:10= "Blue"; Add a new attribute to an already existing object by assigning a value
Mode 2. Using the Object Builder
Just like the constructor in C + +, the function name and the class name are identical
// object builder function person (name, age) { this . Name = name; this . Age = age; // method: Function Inside object this . ChangeName = ChangeName; function ChangeName (name) { this . Name = name; }} // Create instance var myperson = new person ("name", "ten");
Number Object
JavaScript has only one numeric type, all numbers are 64 bits, all floating-point types
- accuracy : integers (up to 15 digits), decimals (up to 17 bits)
- In-process
var y = 0377; 0 opening indicates 8 binary
var x = 0xff;//0x means 16 binary
var z = 10;//defaults to 10 binary
Number.tostring (16);//using the ToString () method to output the number of different binary
Infinity: MyNumber = Infinity; Positive and negative infinity
Non-numeric value: MyNumber = NaN; IsNaN (MyNumber);//Whether it is a Nan value
String Object
Assign value |
var str = "STR";//You can use double quotes var str = ' str ';//can be enclosed in single quotes |
Access |
var character = str[1];//use subscript to access characters in a string |
Length |
var lenth = Str.length;//length property to calculate string length |
Find a location |
var n = str.indexof ("abc");//Position ABC where it first appears in STR, not found returns 1 var str = "AABC"; var obj = Str.match ("abc");//Find the string ABC in STR and return the found result object, matching only once obj result: ["abc", INDEX:1, Input: "AABC", Length:3] [The string to find, the string to look up in str, the input string] |
Replace |
var n = str.replace ("A", "B");//Replace the A in STR with B, replace only the first a, and return the replaced string |
Case-sensitive changes |
var up = Str.touppercase ();//convert All to uppercase var low = Str.tolowercase ();//convert All to lowercase |
Convert an array |
var sstr = Str.split (",");//use String to divide it into groups |
Date Dates Object & nbsp; the month represented by numbers is 1 smaller than the actual number, which is 0 = January
var date1 = new Date ();
var date2 = new Date ();
if (date1 = = date2) {}
get |
var date = new Date () ;//Current date and time var year = Date.getfullyear ();//Get years var day = Date.getday ();//Get date ... |
Create |
var date = new Date (); /Current date and time var date = new Date (1000),//1000 milliseconds after 1970.01.01 8 o'clock in the morning var date = new Date ("October 13, 1975 11:13:00 ");//String to get the date var date = new Date (year, month, day, hours, minutes, seconds, milliseconds); //month=2, last set to March |
set |
var date = new Date (); /p> Date.setfullyear (2010,0,14);//month set to 0, actually 1 |
comparison | TD style= "border:1px solid white" >
array Array object
1. var arr=new Array ();
Arr[0] = 0;arr[1]=1;
2. var arr=new Array (0,1);
3. var arr=[0,1];
Create array | TD style= "border:1px solid white" >
Access array |
arr[0] = 1;//use subscript to access |
Array can have different objects (similar to Python) |
var arr=new Array (); Arr[0] = 0; Arr[1] = "1"; |
Number of elements |
var n = arr.length; |
positioning |
var n = arr.index of (0); |
the Boolean object represents two values of true or false
var B = new Boolean ();//0-1 null "" False undefined Nan means false
the Math object performs common arithmetic tasks
Rounded |
var d = math.round (2.4); |
Random number |
var d = math.random (); |
Maximum/Small value |
var d = Math.max (1,2,3,4); var d = math.min (1,2,3,4); |
Constant |
Math.PI MATH.E math.sqrt (16); square Root 16 Math.sqrt2 Radical 2 math.sqrt1_2 square root 1/2 MATH.LN2 Math.ln10 E as base MATH.LOG2E 2 for Bottom math.log10e 10 |
BOM Browser Object Model
Windows browser window
- Window.screen User screen information, can not be prefixed with window
- Window.location the URL of the current page without a window prefix
- Window.history browser history, without window prefix
- Window.navigator browser information, such as name, version, language, etc., can not be prefixed with window
pop-up window
Warning Box: alert ("no! error! ")
Confirmation Box: var r = comfirm ("please confirm");//Confirm =true Cancel =false
Prompt box: prompt ("input prompt", "default value");//Prompt user to enter information
Timing Events
var interval = setinterval(funname,3000);// loop execution funname function every 3000 milliseconds
Clearinterval (interval);//stop interval loop execution
var timeout = SetTimeOut(Funname, 2000)//After 2000 milliseconds, execute once funname function, do not loop
Cleartimeout (timeout);//Cancel this execution
JS Learning--Object Foundation