A detailed discussion of JavaScript's Object _ Basics

Source: Internet
Author: User
Tags abs arrays

There are a lot of objects in JavaScript, and there are a few more explanations for what you feel.

Learn some books on the web and read JavaScript.

If you want this manual can leave a message, see I will send you. Well, here's a summary of my own object's approach to the next step.

1. Objects:

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

(2) Each object has properties and methods.

(3) JS allows custom objects to be customized.

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>

Results: The interface prints 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>

Results: The interface prints 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>

Results: The interface prints out name: Ying, age:18

String String Object

1.string objects:

A string object is used to work with an existing string, and strings can be spread using either single or double quotes: mixed use to avoid conflicts. 】。

2. Some of the properties of the demo:

(1) Find the string in the 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 a string: 11

World's Location: 6

Location of lllllll:-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 out 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 Hello 123

(4) Uppercase conversion of Strings: toUpperCase ()/toLowerCase ()

Example (for 2-(4)):

<script>

var str= "Hello World";

document.write (Str.touppercase ());

</script>

Result: The interface prints out Hello World

(5) 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>

Results: Interface Printing JJJ

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

Date Date Object

1.Date objects:

Date objects are used to process dates and times.

2. Obtain the date of the day.

3. Some commonly used methods:

(1) getFullYear (): Gets the year.

(2) GetTime (): Gets milliseconds.

(3) setFullYear (): Set 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 Example:

<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);//equal 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>

Results: Dynamic Display time

Array object:

1.Array objects:

Use a separate variable to store a series of values.

2. The creation of the array:

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

3. Array Access:

You can access a particular element by specifying the array name and the index number.

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

4. Common Methods for arrays:

(1) concat (): Merging arrays.

(2) sort (): sorting.

(3) Push (): Append element at end.

(4) reverse (): The element of the array is flipped.

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 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 out a b c e D t

Extension

<script>

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

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

</script>

Results: 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 random number between 0~1.

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

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

(5) ABS (): Returns 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));

Results: The interface prints out 10.

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.