Object of JavaScript

Source: Internet
Author: User

JavaScript has a lot of object content, pick a few feel with more of the explanation.

Learn a few and read JavaScript manuals online.

If you want this manual can leave a message, see I will send you. Well, let's summarize my own way of going to the next step.

--------------------------------------------------------------------------------------------

1. Object:

(1) All events in JavaScript are objects: strings, arrays, functions .....

(2) Each object has properties and methods.

(3) JS allows you to customize the object.

2. Custom objects:

(1) Define and create an object instance.

(2) Use a function to define the object, and then create a new object instance.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example (for 2-(1)):

Method 1:

<script>

People=new Object ();

People.name= "Ying";

People.age= "18";

document.write ("Name:" +people.name+ ", Age:" +people.age);

</script>

Result: Interface print out name: Ying, age:18

Method 2:

<script>

People=new Object ();

people={

Name: "Ying",

Age:18

}

document.write ("Name:" +people.name+ ", Age:" +people.age);

</script>

Result: Interface print out name: Ying, age:18

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~··

Example (for 2-(2)):

<script>

function people (Name,age) {

This.name=name;

This.age=age;

}

Son=new people ("Ying", 18);

document.write ("Name:" +son.name+ ", Age:" +son.age);

</script>

Result: Interface print out name: Ying, age:18

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

------------------------------------------------------------------------

String String Object

1.string objects:

String objects are used to handle existing strings, and strings can use single or double quotes "spread: Mixed use to avoid collisions." 】。

2. Some properties of the demo:

(1) Finding a string in a string: IndexOf ()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example (for 2-(1)):

<script>

var str= "Hello World";

document.write ("string:" +str.length+ "<br/>");

document.write ("World Position:" +str.indexof ("World") + "<br/>");

document.write ("llllll position" Str.indexof ("llllll"));

</script>

Result: The interface prints out the string: 11

World's Position: 6

Lllllll's Location: 1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(2) Content matching: match ()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example (for 2-(2)):

<script>

var str= "Hello World";

document.write (Str.match ("World"));

document.write (Str.match ("lllllll"));

</script>

Result: The interface prints world null

~~~~~~~~~~~~~~~~~~~~~~~

(3) Replacement content: replace ()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example (for 2-(3)):

<script>

var str= "Hello World";

document.write (Str.replace ("World", "123"));

</script>

Result: The interface prints out Hello 123

~~~~~~~~~~~~~~~~~~~~~

(4) string capitalization conversion: toUpperCase ()/toLowerCase ()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example (for 2-(4)):

<script>

var str= "Hello World";

document.write (Str.touppercase ());

</script>

Result: The interface prints out Hello World

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(5) Convert string to array: Split ()

~~~~~~~~~~~~~~~~~~~~~~

Example (for 2-(5)):

<script>

var str1= "HELLO,JJJ,LLL,KKK";

var s=str1.split (",");//comma as delimiter

document.write (S[1]);

</script>

Result: Interface Print JJJ

~~~~~~~~~~~~~~~~~~~~~~~~

----------------------------------------------------

Date Day Object

1.Date objects:

Date objects are used to process dates and times.

2. Obtain the date of the day.

3. Some common methods:

(1) getFullYear (): Gets the year.

(2) GetTime (): Gets the milliseconds.

(3) setFullYear (): Set a specific date.

(4) GetDay (): Get week.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example (for 3):

<script>

var date = new Date ();

document.write (date+ "<br/>");

document.write (Date.getfullyear () + "<br/>");

document.write (Date.gettime () + "<br/>");

document.write (Date.getday () + "<br/>");

Date.setfullyear (2010,1,1);

document.write (date);

</script>

Results:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

4. Clock instance:

<script >
function StartTime () {
var today=new Date ();
var h=today.gethours ();
var m=today.getminutes ();
var s=today.getseconds ();
M=checktime (m);
S=checktime (s);
document.getElementById (' txt '). innerhtml=h+ ":" +m+ ":" +s;
T=settimeout (' StartTime () ', 500);//equivalent to T=settimeout (function () {startime;},500)
}

function Checktime (i) {
if (i<10) {

I= "0" + i;

}
return I
}
</script>

<body onload= "StartTime ()" >
<div id= "TXT" ></div>
</body>

Result: Dynamic display time

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------------

Array object:

1.Array objects:

Use a separate variable to store a series of values.

2. Creation of arrays:

Example: Var myarray=["KKK", "ddd", "ddddd"];

3. Access to arrays:

By specifying the array name and the index number, you can access a particular element.

Note: [0] is the first element of the array, and so on. 】

4. Common methods of arrays:

(1) concat (): Merges an array.

(2) sort (): sort.

(3) Push (): Appends the element to the end.

(4) reverse (): array element rollover.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example (for 4-(1)):

<script>

var a=["AA", "BB"];

var b=["CC", "DD"];

var c=a.concat (b);

document.write (c);

</script>

Result: The interface prints out the AA bb cc DD

Example (for 4-(2)):

<script>

var a=["A", "C", "D", "T", "B", "E"];

document.write (A.sort ());

</script>

Result: The interface prints a B c e D t

Extension

<script>

var a=["5", "2", "3", "4", "1"];

document.write (A.sort (A, b) {return b-a;})

</script>

Result: The interface prints out 54321. (PS: Because b-a is in reverse order).

Example (for 4-(3)):

<script>

var a=["A", "B"];

A.push (c);

document.write (a);

</script>

Result: Print out ABC

Example (for 4-(4)):

<script>

var a=["C", "B", "a"];

document.write (A.reverse ());

</script>

Result: Print out ABC

~~~~~~~~~~~~~~~~~~~~~~~~~

--------------------------------------------------------------------

Math object:

1.Match objects:

Perform common arithmetic tasks.

2. Common methods:

(1) Round (): rounded.

(2) Random (): Returns the number of 0~1 between.

(3) Max (): Returns the highest value.

(4) min (): Returns the minimum value.

(5) ABS (): Returns the absolute value.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·

Example (for 2-(1)):

document.write (Math.Round (2.5));

Result: The interface prints out 3

Example (for 2-(2)):

document.write (Math.randow ());

Results: The interface randomly prints out a number of 0~1.

document.write (Math.randow () *10);

Results: The interface randomly prints out a number of 1~10.

document.write (parseint (Math.randow ()));

Result: The interface randomly prints out a number of 0~1, and this number is an integer.

Example (for 2-(3)):

document.write (Math.max (10,20,3,90));

Result: The interface prints a maximum value of 90.

Example (for 2-(4)):

document.write (Math.min (12,0,2,3,4));

Result: The interface prints a minimum value of 0.

Example (for 2-(5)):

document.write (Math.Abs (-10));

Result: The interface prints out 10.

Object of JavaScript

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.