JS Object
Object Builder
function Person (firstname,lastname,age,eyecolor) {this.firstname=firstname;this.lastname=lastname;this.age=age; This.eyecolor=eyecolor;} Myfather=new person ("John", "Doe", "Blue");
Or
Person={firstname: "John", LastName: "Doe", Age:50,eyecolor: "Blue"};
AvascriptNumber Object
JavaScript has only one numeric type.
You can use it or you can write a number without using a decimal point.
The maximum or minimum number can be written by means of the scientific (exponential) notation:
Example Var y=123e5; 12300000
var z=123e-5; 0.00123 Accuracy
Integers (not using decimal or exponential notation) are up to 15 bits.
Instancevar x = 999999999999999; // x for 999999999999999 var y = 9999999999999999; // y for 10000000000000000
Try it?
The maximum number of decimal digits is 17, but the floating-point operation is not always 100% accurate:
instance var x = 0.2+0.1; Output is 0.30000000000000004
Try it? Octal and hexadecimal
If the prefix is 0, JavaScript interprets numeric constants as octal numbers, and if the prefixes are 0 and "X", they are interpreted as hexadecimal numbers.
var y = 0377; var z = 0xFF;
By default, JavaScript numbers are displayed in decimal.
But you can use the ToString () method to output 16 binary, 8 binary, and 2 binary.
Example Var mynumber=128;
Mynumber.tostring (16); Returns 80
Mynumber.tostring (8); Returns 200
Mynumber.tostring (2); Returns 10000000var x = 123;
var y = new number (123);
typeof (X)//return number
typeof (Y)//return ObjectjavascriptString Object
You can use the escape character (\) in the string to quote:
Using the Length property
lengthTo calculate the length of a string:
var txt="Hello world! "; // String var txt1=txt.touppercase (); // txt1 text is converted to uppercase var txt2=txt.tolowercase (); // txt2 text is converted to lowercase
Merging two arrays-concat ()
Merging three arrays-concat ()
Make a string of elements of an array-join ()
Delete the last element of an array-pop ()
Add a new element to the end of the array-push ()
Reverses the order of the elements in an array-reverse ()
Delete the first element of an array-shift ()
Select an element from an array-slice ()
Array sort (ascending alphabetical order)-sort ()
Sort by number (ascending in numeric order)-sort ()
Sort by number (descending in numeric order)-sort ()
Add an element to the 2nd position of the array-splice ()
Convert array to string-tostring ()
Add a new element at the beginning of the array-unshift ()
If the Boolean object has no initial value or its value is:
- 0
- -0
- Null
- ""
- False
- Undefined
- NaN
Then the value of the object is false. Otherwise, its value is true, even when the value of the variable is the string "false"!
The following example uses the floor () method and random () of the Math object to return a random number between 0 and 10:
document.write (Math.Round (Math.random () *10));JavascriptRegExp Object
REGEXP: is a shorthand for regular expressions (regular expression).
Syntax var patt=new RegExp (pattern,modifiers);
or an easier way
var patt=/pattern/modifiers;
Note: When you use constructors to create regular objects, you need a regular character escaping rule (preceded by a backslash \). For example, the following are equivalent:
var=newRegExp("\\w+"); var=/\w+/;
REGEXP modifier
Modifiers are used to perform case-insensitive and full-text searches.
The i -modifier is used to perform a case-insensitive match.
The g -modifier is used to perform a full-text search (instead of finding the first one to stop looking, but to find all matches).
var str= "is the There is?"; var patt1=/is/gi;document.write (str.match(PATT1)); =============is,is,is
Test ()
The test () method searches for the value specified by the string, depending on the result and returns TRUE or false.
The following example searches for the character "E" from a string:
var patt1=new RegExp("e");//or Var patt1=/e/;
document. Write(patt1. Test("The best things in life is free");
EXEC ()
The EXEC () method retrieves the specified value in the string. The return value is the value that was found. If no match is found, NULL is returned.
<script>var patt1=/e/IG;d ocument.write (patt1.exec ("Best things on Life is free");</script> =========e// only printed an e
JavaScript Objects (review notes)