There are many object contents in JavaScript, and I can choose a few that I use more to explain.
Learn some online and read javascript study manuals.
If you want this manual, you can leave a message, and I will send it to you if you see it. Well, let’s summarize my own path to advancement.
1. Object:
(1) All events in javascript are objects: strings, arrays, functions...
(2) Each object has properties and methods.
(3) JS allows custom objects.
2. Custom objects:
(1) Define and create an object instance.
(2) Use functions to define the object, and 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 out 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 out 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 out name: Yan Xiaoyuan, age:18
string string object
1.string object:
string object is used to process existing strings; strings can be used in single or double quotes [Expand: mixed use to avoid conflicts. 】.
2. Demonstration of some properties:
(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's location:"+str.indexOf("world")+"<br/>");
document.write("llllll's location"str.indexOf("llllllll"));
</script>
Result: The interface prints out the string: 11
The location of world: 6
llllllll'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("llllllll"));
</script>
Result: The interface prints out world null
(3) Replace content: replace()
Example (for 2-(3)):
<script>
var str="hello world";
document.write(str.replace("world","123"));
</script>
Results: The interface prints hello 123
(4) String capitalization conversion: toUpperCase() / toLowerCase()
Example (for 2-(4)):
<script>
var str="hello world";
document.write(str.toUpperCase());
</script>
Results: 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(",");// Use commas as the separator
document.write(s[1]);
</script>
Result: Interface printing jjj
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Date date object
1.Date object:
Date objects are used to process dates and times.
2. Obtain the date of the day.
3. Some commonly used methods:
(1) getFullYear(): Get the year.
(2) getTime(): Get milliseconds.
(3) setFullYear(): Set the 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 id="txt"></div>
</body>
</html>
Results: Dynamic display time
Array object:
1.Array object:
Use separate variables to store a series of values.
2. Array creation:
Example: var myArray=["kkk","ddd","ddddd"];
3. Access to arrays:
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. Common methods for arrays:
(1) concat(): Merge arrays.
(2) sort(): sort.
(3) push(): Append element at the end.
(4) reverse(): Flip the array element.
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 abcedt
【Extended】
<script>
var a=["5","2","3","4","1"];
document.write(a.sort(function(a,b){return ba;}))
</script>
Result: The interface prints out 54321. (ps: because ba is arranged in reverse order).
Example (for 4-(3)):
<script>
var a=["a","b"];
a.push(c);
document.write(a);
</script>
Results: Print out abc
Example (for 4-(4)):
<script>
var a=["c","b","a"];
document.write(a.reverse());
</script>
Results: Print out abc
Math object:
1.Match object:
Perform common arithmetic tasks.
2. Common methods:
(1) round(): 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));
Results: 3 printed on the interface
Example (for 2-(2)):
document.write(Math.randow());
Result: The interface randomly prints a number of 0~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 out the minimum value of 0.
Example (for 2-(5)):
document.write(Math.abs(-10));
Result: The interface prints out 10.