Array object
The Array object is used to store multiple values in a single variable.
Syntax for creating arrays:
new Array(a);new Array(size);
new Array(element0, element1, ... , elementn);Copy the code
Parameters:
- The size argument is the expected number of array elements. The length field will be set to the value of size.
- Parameter element… , elementn is the parameter list. When the constructor Array() is called with these parameters, the elements of the newly created Array are initialized to these values. Its length field is also set to the number of arguments.
The return value:
- Returns the newly created and initialized array.
- If the constructor Array() is called with no arguments, the Array returned is empty and the length field is 0.
- The constructor returns an array of undefined elements with a specified number when it is passed a numeric argument.
- When other arguments call Array(), this constructor initializes the Array with the value specified by the argument.
- When a constructor is called as a function without the new operator, it behaves exactly as it would when called with the new operator.
Array Object properties:
- Constructor: Returns a reference to the array function that created this object.
Grammar:
object.constructor
Copy the code
Example:
<script type="text/javascript">
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates"."Engineer".1985);
document.write(bill.constructor);
</script>
Copy the code
- Length: Sets or returns the number of elements in the array.
Syntax: Sets the number of arrays
array.length=number
Copy the code
- Prototype: Adds attributes and methods to an object.
When building a property, all arrays will be set to the property, which is the default. When you build a method, all arrays can use that method.
Grammar:
Array.prototype.name=value
Copy the code
Example:
<script>
Array.prototype.myUcase=function()
{
for (i=0; i<this.length; i++) {this[i]=this[i].toUpperCase(); }}function myFunction()
{
var fruits = ["Banana"."Orange"."Apple"."Mango"];
fruits.myUcase();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
Copy the code
Array object methods:
- Concat () : Joins two or more arrays and returns the result.
- Join () : Puts all the elements of an array into a single string. Elements are separated by the specified delimiter.
- Pop () : Removes and returns the last element of the array
- Shift () : Removes and returns the first element of the array
- Splice () : Deletes elements and adds new elements to the array.
- Reverse () : Reverses the order of elements in an array.
- Sort () : Sorts the elements of an array
- Push () : Adds one or more elements to the end of the array and returns the new length.
- Slice () : Returns the selected element from an existing array
- ToSource () : Returns the source code for this object.
- ToString () : Converts an array to a string and returns the result.
- ToLocaleString () : Converts an array to a local array and returns the result.
- Unshift () : Adds one or more elements to the beginning of the array and returns the new length.
- ValueOf () : returns the original valueOf an array object
String object:
The String object is used to process text (strings).
Syntax for creating strings:
new String(s);
String(s);
Copy the code
Parameter: The parameter S is a value to be stored in a String or converted to a raw String.
The return value:
- When String() is used as a constructor with the operator new, it returns a newly created String containing the String s or a String representation of s.
- When String() is called without the new operator, it simply converts s to the original String and returns the converted value.
String object properties:
- Constructor: a reference to the function that created the object
Grammar:
string.constructor
Copy the code
Example:
<script>
var txt = "Hello World!";
document.write(txt.constructor);
</script>
Copy the code
- Length: indicates the length of the string
Grammar:
string.length
Copy the code
Example:
<script>
var txt = "Hello World!";
document.write(txt.length);
</script>
Copy the code
- Prototype: Allows you to add properties and methods to an object. (Prototype is global and applies to all Javascript objects)
Grammar:
object.prototype.name = value
Copy the code
Example: Apply the Prototype attribute to an object
<script>
function employee(name,jobtitle,born){
this.name=name;
this.jobtitle=jobtitle;
this.born=born;
}
var fred=new employee("Fred Flintstone"."Caveman".1970);
employee.prototype.salary=null;
fred.salary=20000;
document.write(fred.salary);
</script>
Copy the code
String methods:
- Anchor () : Creates an HTML anchor.
- Big () : Displays strings in large font.
- Blink () : displays flashing strings.
- Bold () : Displays the string in bold.
- CharAt () : Returns the character at the specified position.
- CharCodeAt () : Returns the Unicode encoding of the character at the specified position.
- Concat () : concatenation string.
- Fixed () : Displays string as typewriter text.
- Fontcolor () : displays the string using the specified color.
- Fontsize () : displays the string using the specified size.
- FromCharCode () : Creates a string from the character encoding.
- IndexOf () : Retrieves strings.
- Italics () : displays strings in italics.
- LastIndexOf () : Searches strings from back to front.
- Link () : Displays strings as links.
- LocaleCompare () : Compares two strings in a locally specific order.
- Match () : Finds a match for one or more regular expressions.
- Replace () : Replaces the substring that matches the regular expression.
- Search () : Retrieves the value that matches the regular expression.
- Slice () : Extracts a fragment of a string and returns the extracted portion in a new string.
- Small () : Displays strings with small font size.
- Split () : Splits a string into an array of strings.
- Strike () : Displays a string using a stripper.
- Sub () : Displays strings as subscripts.
- Substr () : Extracts a specified number of characters from the starting index in the string.
- Substring () : Extracts the character between two specified index numbers in a string.
- Sup () : displays strings as superscripts.
- ToLocaleLowerCase () : Converts the string to lowercase.
- ToLocaleUpperCase () : Converts the string to uppercase.
- ToLowerCase () : converts the string toLowerCase.
- ToUpperCase () : converts the string toUpperCase.
- ToSource () : Represents the source code for the object.
- ToString () : Returns a string.
- ValueOf () : returns the original valueOf a string object.
The Data object
The Date object is used to handle dates and times.
Syntax for creating Date objects:
var myDate=new Date(a)Copy the code
- The Date object automatically saves the current Date and time as its initial value.
Date object attributes:
- Constructor: Returns a reference to the Date function that created this object.
Grammar:
object.constructor
Copy the code
Example:
<script type="text/javascript">
var test=new Date(a);if (test.constructor==Array) {document.write("This is an Array");
}
if (test.constructor==Boolean) {document.write("This is a Boolean");
}
if (test.constructor==Date) {document.write("This is a Date");
}
if (test.constructor==String) {document.write("This is a String");
}
</script>
Copy the code
- Prototype gives you the ability to add properties and methods to objects.
Grammar:
object.prototype.name=value
Copy the code
Example:
<script type="text/javascript">
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates"."Engineer".1985);
employee.prototype.salary=null;
bill.salary=20000;
document.write(bill.salary);
</script>
Copy the code
Date object method:
- Date() : Returns the Date and time of the day.
- GetDate () : Returns a day (1-31) in a month from the Date object.
- GetDay () : Returns a day of the week (0 to 6) from the Date object.
- GetMonth () : returns the month (0 to 11) from the Date object.
- GetFullYear () : Returns the year as four digits from the Date object.
- GetYear () : Use the getFullYear() method instead.
- GetHours () : Returns the hour of the Date object (0 ~ 23).
- GetMinutes () : Returns the minutes of the Date object (0 to 59).
- GetSeconds () : Returns the number of seconds (0 to 59) of the Date object.
- GetMilliseconds () : returns the milliseconds (0 to 999) of the Date object.
- GetTime () : returns the number of milliseconds since January 1, 1970.
- GetTimezoneOffset () : Returns the minute difference between the local time and the Greenwich Mean Time (GMT).
- GetUTCDate () : returns the day of the month (1-31) from the Date object based on world time.
- GetUTCDay () : returns the day of the week (0 to 6) from the Date object based on world time.
- GetUTCMonth () : returns the month (0 to 11) from the Date object based on world time.
- GetUTCFullYear () : Returns a four-digit year from the Date object based on world time.
- GetUTCHours () : returns the hour of the Date object based on world time (0 ~ 23).
- GetUTCMinutes () : Returns the minute of the Date object based on universal time (0 to 59).
- GetUTCSeconds () : Returns the number of seconds (0 to 59) of the Date object based on universal time.
- GetUTCMilliseconds () : Returns the milliseconds (0-999) of the Date object according to the world time.
- Parse () : Returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string).
- SetDate () : Sets the Date of the month (1 to 31) in the Date object.
- SetMonth () : Sets the month (0 to 11) in the Date object.
- SetFullYear () : Sets the year (four digits) in the Date object.
- SetYear () : Use setFullYear() instead.
- SetHours () : Sets the hour (0 ~ 23) in the Date object.
- SetMinutes () : Sets the minutes (0 to 59) in the Date object.
- SetSeconds () : Sets the number of seconds (0-59) in the Date object.
- SetMilliseconds () : sets milliseconds (0 to 999) in the Date object.
- SetTime () : Sets the Date object in milliseconds.
- SetUTCDate () : sets the day (1-31) of the month in the Date object based on universal time.
- SetUTCMonth () : sets the month (0 to 11) in the Date object based on universal time.
- SetUTCFullYear () : Sets the year (four digits) in the Date object according to universal time.
- SetUTCHours () : Sets the hour (0 ~ 23) in the Date object according to world time.
- SetUTCMinutes () : sets the minutes (0 to 59) in the Date object based on universal time.
- SetUTCSeconds () : Sets the seconds (0 to 59) in the Date object based on universal time.
- SetUTCMilliseconds () : Sets the milliseconds (0 to 999) in the Date object according to the universal time.
- ToSource () : Returns the source code for this object.
- ToString () : Converts a Date object to a string.
- ToTimeString () : Converts the time part of the Date object to a string.
- ToDateString () : Converts the Date part of the Date object to a string.
- ToGMTString () : Use the toUTCString() method instead.
- ToUTCString () : Converts the Date object to a string based on world time.
- ToLocaleString () : Converts a Date object to a string based on the local time format.
- ToLocaleTimeString () : Converts the time part of the Date object to a string based on the local time format.
- ToLocaleDateString () : Converts the Date part of the Date object to a string according to the local time format.
- ValueOf () : returns the original valueOf the Date object.