当前位置: 代码迷 >> JavaScript >> javascript惯用对象
  详细解决方案

javascript惯用对象

热度:327   发布时间:2013-10-08 16:55:16.0
javascript常用对象

1.Date对象:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<script type="text/javascript">
  var date = new Date();
  document.write(date);
</script>
 </head>
 <body>
 </body>
</html>


运行结果:

我们来改变一下输出的方式:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<script type="text/javascript">
  var date = new Date();
  document.write(date.getFullYear()+"年"+date.getMonth()+"月"+date.getDate()+"日");
</script>
 </head>
 <body>
 </body>
</html>


运行结果:

明明是9月,却显示的是8月,说明getMonth是从下标0开始的,所以要加1:

 document.write(date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日");


这样就会输出正确的结果了。

 

2.String对象:

2.1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<script type="text/javascript">
  var str1 = new String("niujiabin");
  var str2 = "niujiabin";
  alert(str1==str2);
</script>
 </head>
 <body>
 </body>
</html>


结果是true,这里与java是有区别的,并不存在引用,没有equals方法。

 

 

2.2字符串连接

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<script type="text/javascript">
  var str1 = new String("niujiabin");
  var str2 = "niujiabin";
//  alert(str1==str2);
//字符串的连接
var str3 = str2.concat("maybe","gossip");
alert(str3);
</script>
</head>
<body>
</body>
</html>


连接字符串,concat的参数没有限制,随便连就可以了,输出结果:

 

 

2.3字符串截取

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">
//字符串截取
var str1 = "niujiabinbin";
var str2 = str1.slice(3,6);
alert(str2);
</script>
 </head>

 <body>
  
 </body>
</html>


运行结果:

 

2.4字符串选取substring与substr

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">
   var str1 = "niujiabinbin";
   document.write(str1.substring(2,9)+"<br>");
   document.write(str1.substr(2,9));
</script>
 </head>

 <body>
  
 </body>
</html>


运行结果:

区别在于substring是下标2-9,而substr是从下标2开始,选取9个字符。

 

3.array对象:

js中的array对象其实就是java中的List和Stack集合

3.1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">
  //第一种定义方式
  var a1 =new Array();
  a1.push(11);
  a1.push(12);
  document.write(a1+"<br>");

  //第二种定义方式
  var a2 = new Array("niujiabin","1",1);
  document.write(a2+"<br>");
   
  //第三种定义方式,一般都采用这种方式
  var a3=["maybe",1,"1"];
  document.write(a3+"<br>");
</script>
 </head>
 <body>  
 </body>
</html>


输出结果:

 

输出结果的分割形式都是逗号,我们当然也可以改变,只需通过join方法:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">
  //第一种定义方式
  var a1 =new Array();
  a1.push(11);
  a1.push(12);
  document.write(a1.join("-")+"<br>");

  //第二种定义方式
  var a2 = new Array("niujiabin","1",1);
  document.write(a2.join("_")+"<br>");
   
  //第三种定义方式,一般都采用这种方式
  var a3=["maybe",1,"1"];
  document.write(a3.join("***")+"<br>");
</script>
 </head>
 <body>  
 </body>
</html>


运行结果:

 

3.2 sort方法:

这个方法只会根据字符串来排序:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">

  var a3=[1111,111,12,443,44,555,5555,67,76];
  document.write(a3.sort());
</script>
 </head>
 <body>  
 </body>
</html>


排序结果:

 

array的方法还有pop,shift,onshift等对栈和队列的相关操作,还有splice插入元素,这里就不一一介绍了。

  相关解决方案