Home>

Object is a composite value,It aggregates many values ​​(primitive values ​​or other objects),These values ​​can be accessed by attribute name.The attribute name can be any string including an empty string.JavaScript objects can also be called a data structure

There are many object contents in javascript,Pick more feelings to explain more.

Learn some online and read the javascript study manual.

If i want to leave a message for this manual,I will send it to you when I see it.Well, let's summarize the way of my own object.

1. Object:

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

(2) Each object has attributes and methods.

(3) js allows custom objects.

2. Custom object:

(1) Define and create object instances.

(2) Use functions to define objects,Then create a new object instance.

Example (for 2- (1)):

method 1:

<script>

people=new object ();

people.name="Yan Xiaoyuan";

people.age="18";

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

</script>

Result:the interface prints name:Yan Xiaoyuan, age:18

Method 2:

<script>

people=new object ();

people={

name:"Yan Xiaoyuan",

age:18

}

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

</script>

Result:the interface prints name:Yan Xiaoyuan, age:18

Example (for 2- (2)):

<script>

function people (name, age) {

this.name=name;

this.age=age;

}

son=new people ("Yan Xiaoyuan", 18);

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

</script>

Result:the interface prints name:Yan Xiaoyuan, age:18

string object

1.string object:

The string object is used to process existing strings;Strings can use single or double quotes [extension:mixed use to avoid conflicts.】.

2. Demonstration of some attributes:

(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 ("Location of the world:" + str.indexof ("world") + "

document.write ("location of llllll" str.indexof ("llllll"));

</script>

Result:the interface prints out the string:11

world's location: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 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 string conversion:touppercase ()/tolowercase ()

Example (for 2- (4)):

<script>

var str="hello world";

document.write (str.touppercase ());

</script>

Result:the interface prints 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 separator

document.write (s [1]);

</script>

Result:Interface prints jjj

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

date object

1.date object:

Date objects are used to handle dates and times.

2. Get the date of the day.

3. Some common methods:

(1) getfullyear ():Get the year.

(2) gettime ():Get milliseconds.

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

(4) getday ():Get the 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>

result:

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

4. Clock example:

<html>

<head>

<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>

</head>

<body onload="starttime ()">

<div></div>

</body>

</html>

Result:Dynamic display time

array object:

1.array object:

Use separate variables to store a range of values.

2. Array creation:

Example:var myarray=["kkk", "ddd", "ddddd"];

3. Array access:

By specifying the array name and index number,You can access a specific element.

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

4. Array common methods:

(1) concat ():merge array.

(2) sort ():sort.

(3) push ():Appends an element to the end.

(4) reverse ():Array elements are reversed.

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 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

[Expand]

<script>

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

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

</script>

Result:the interface printed 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 object:

Perform common arithmetic tasks.

2. Common methods:

(1) round ():rounded.

(2) random ():Returns a random number between 0 and 1.

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

Result:The interface randomly prints a number from 0 to 1.

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

Result:The interface randomly prints a number from 1 to 10.

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

Result:The interface randomly prints a number from 0 to 1.And this number is an integer.

Example (for 2- (3)):

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

Result:the interface prints the maximum value of 90.

Example (for 2- (4)):

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

Result:The interface prints the minimum value of 0.

Example (for 2- (5)):

document.write (math.abs (-10));

Result:the interface prints 10.

  • Previous Fifty-nine of manipulating data in ASPNET 20: using SQL cache dependency SqlCacheDependency
  • Next Detailed explanation and simple examples of AngularJS ng-change directive