关于JavaScript的一些知识

函数的两种定义:
1.可以无函数名的定义
   var add=function(){}
2.有函数名的定义
    function add(){}
函数的调用有四种:
1.简单的调用,
  add();
2.在表达式中调用
  document.write(add());
3.在事件中调用
  <input type="button" οnclick="add()" value="确定"/>
4.在链接中调用
  <a href="javascript:add()">相加</a>
特殊的函数有三种
1.内置函数
2.递归函数
3.嵌套函数
JavaScript中的7个内置函数
1.eval()
这个函数是有参数的,如果参数是JavaScript的表达式,如果参数是字符串,并且包含有数值的,有确定的值那么就会返回一个表达式的数值,如果不是确定的数值就会返回undefined。
2.isFinite()
这个函数的参数是可选的,当参数是非数字,无穷大,或者无穷小则返回false,其他的就返回true,特别的当参数是数值型的字符串时,就会自动将字符串转化为数字。
3.isNaN()
这个函数是用来判断参数是否不是数值,当参数不是数值的时候会返回false,当参数是数值的时候就会返回true,当参数是数值字符串时,会自动将字符串转化为数值,而现实false
4.parseInt()和parseFloat()
parseInt()将其他类型转化为整形的,当为非数值的时候,会返回NaN值,但是当字符串首个字符为数值型时,就会返回该数值。比如a="360cn",b="3",a=parseInt(a);则a+b=363,这个是有返回值的,大家一定要注意!!!
parseFloat()这个函数是将其他类型的转化为浮点型的
5.escape()和unescape()
这两个函数一个是对字符串进行编码另一个就是对其进行解码
对于字符串有三个类似的函数来检测是否存在
1.match()
使用方法:原字符串.match("要检测的字符串")
如果检测的字符串存在,就会返回原字符串,否则就会返回null
2.search()
使用方法:原字符串.search("要检测的字符串")
如果检测到有原字符串,会返回字符串首个字符的位置,否则返回-1
3.indexOf()
使用方法:原字符串.indexOf("要检测的字符串")
如果检测到有原字符串,会返回首个匹配的字符串首个字符的位置,否则返回-1
在这里,小编试了一下,search和indexOf是没有什么区别的,代码如下


<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题</title>
<script type="text/javascript">
     function mymatch(){
      var s="hello world!hello world!hello world!hello world!hello world!hello world!";
      document.write(s.match("helllo")+"<br/>");
      document.write(s.match("hello")+"<br/>");
      document.write(s.match("world!")+"<br/>");
     }
    function mysearch(){
      var s="hello world!hello world!hello world!hello world!hello world!";
      document.write(s.search("helllo")+"<br/>");
      document.write(s.search("hello")+"<br/>");
      document.write(s.search("world!")+"<br/>");
     
     }
    function myindexof(){
      var s="hello world!hello world!hello world!hello world!hello world!hello world!";
      document.write(s.indexOf("helllo")+"<br/>");
      document.write(s.indexOf("hello")+"<br/>");
      document.write(s.indexOf("world!")+"<br/>");
     
     }
</script>
</head>
<body>
  <input type="button" οnclick="mymatch()" value="检测match">
  <input type="button" οnclick="mysearch()" value="检测search">
  <input type="button" οnclick="myindexof()" value="检测indexof">
</body>
</html>
其中match返回的是
null
hello
world!
而search和indexOf方法返回的都是
-1
0
6


对于字符串经常用到的一些函数
1.charAt(n)
这个函数是用来显示字符串的第n+1个字符的
2.replace(原字符串,将要替换的字符串)
函数比较简单,从它的意思就可以明白,不再做解释
3.大小写转化的两个函数toUpperCase()和toLowerCase()
4.比较字符串的大小:localCompare()
使用方法:字符串1.localCompare(字符串2)
如果字符串1大于字符串2则返回1,字符串1小于字符串2返回0,字符串1和字符串2相等则会返回0
5.字符串切割函数:split()
使用方法:字符串.split("分隔符")
它将返回一个被分割符分开的数值,但是分隔符不会返回,在原字符串中空格也作为字符的一部分,在分割时会显示出来
6.提取字符串函数:substring()
使用方法:原字符串.sustring(a,b)
要注意的是字符串的首个字符是以0开始的,第2个字符其实是从1开始提取的,另外这个函数书写一定要注意全部是小写的,不要写错了
下面是用于数组的一些函数
1.slice()
用法:原数组.slice(a,b)
提取数组的某些子元素,标号是从0开始的,a是它的起始提取内容,b是它的最后提取内容
2.unshift()和shift(),push()和pop,
用法:数组.unshift(“添加内容”。。。),数组.push(“添加内容”。。。),数组.shift(),数组.pop(。。。)
前两个是在首位置添加元素或删除元素,而后两个是在末尾添加或删除元素,但是添加和删除是有区别的,对于unshift和push函数来说是直接添加到数组中的,但是如果a=arr.shift(),则a显示的是要删除的内容,即为首个元素,同样的,a=arr.pop(),则a是数组的最后一个元素
3.sort()
使用方法:sort(asc),sort(des)
第一个是升序方法,第二个是降序方法
4.join()和contact()
使用方法:arr.join("*") arr.contact("字符串1","字符串2"。。。)
join方法是将字符数组中每个元素添加*后连接起来,而contact是将多个字符串连接起来
5.reverse()
反向排序函数
对于定时器,发现一个问题,代码如下:
<!doctype <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题</title>
<script type="text/javascript">
    var n=0;
    window.οnlοad=function(){
    var t=setInterval("add()",1000);
        document.getElementById("mystop").οnclick=function (){
        clearInterval(t);
        }
        document.getElementById("mycontinue").οnclick=function(){
                t=setInterval("add()",1000);
        }


    }
    function add(){
    n++;
    document.getElementById("nnow").innerHTML=n;
    }
    
</script>
</head>
<body>
<p> 现在的数字是:<span id="nnow">0</span></p>
<input type="button" value="继续" id="mycontinue"/>
<input type="button" value="停止" id="mystop"/>
</body>
</html>
这时候运行是正常的,但是当我去掉document.getElementById("mycontinue").οnclick=function(){
                t=setInterval("add()",1000);
        }
中的
t=setInterval("add()",1000);
中的“t=”的时候,停止按钮就不会显示它的作用,这个是需要注意的
发布了19 篇原创文章 · 获赞 0 · 访问量 9422

猜你喜欢

转载自blog.csdn.net/yuxinqingge/article/details/50902496
今日推荐