JavaScript-引入方式、数据类型

来源:互联网 发布:sql泰勒公式 编辑:程序博客网 时间:2024/05/24 00:46

引入方式

  1. 页头引入(head)
  2. 页中引入(body)
  3. 元素事件中引入(标签)
  4. 外部引入(link)

实例

<!DOCTYPE HTML><html>    <head>        <title>JavaScript引入方式<title>        <script type="text/javascript" src="function.js">/*外部引入*/</script>        <script type="text/javascript">            /*页头引入*/        </script>    </head>    <body>        <script type="text/javascript">            /*页中引入*/        </script>        <input type="button" onclick="alert('元素事件中引入')" value="元素事件中引入"></input>    </body></html>

数据类型

字符串(”content”)、数字(1)、布尔(true/false)、数组(Array)、对象、Null、Undefined
相比较于Java,多了一个undefined,相当于是没有初始化的值;但是在JavaScript中声明变量只能使用var,在函数(方法、function)传参的时候也不需要声明类型(可看作都是var,写不写都认识吧!),并且方法上要求传参,但是调用的时候不传也能调用该方法!

<!DOCTYPE HTML><html>    <head>        <meta charset="utf-8"/><!--编码格式为utf-8-->        <title>js数据类型</title>        <script type="text/javascript">            function test1(){                document.write("字符串<br/>");                var a = "hello";                var b = "world";                document.write(a+b);                document.write("<br/>类型  "+typeof(a+b));            }            test1();            document.write("<hr/>");            function test2(){                document.write("数字<br/>");                var a = 1;                var b = 2;                var c = "2";//c是一个字符串                document.write(a+"+"+b+"="+(a+b));//3                document.write("<br/>类型  "+typeof(a+b));                document.write("<br/>");                document.write("数字"+a+"+"+"字符串"+c+"="+(a+c));//12                document.write("<br/>类型  "+typeof(a+c));            }            test2();            document.write("<hr/>");            function test3(){                document.write("布尔<br/>");                var a = true;                var b = false;                document.write(a);                document.write("<br/>类型  "+typeof(a));                document.write("<br/>");                document.write(b);            }            test3();            document.write("<hr/>");            function test4(){                document.write("数组<br/>");                var a = new Array(1,2,"3");//相当于一个object数组                for(i in a){//遍历,可以看成增强for循环                    document.write(a[i]+"  ");                }                document.write("<br/>类型  "+typeof(a));            }            test4();            document.write("<hr/>");            function test5(){                document.write("对象<br/>");                var person = {id : "1001",name : "张三"};                var id = person["id"];//第一种获取属性的方式                document.write(id+"--");                var name = person.name;//第二种获取属性的方式                document.write(name);                document.write("<br/>类型  "+typeof(person));            }            test5();            document.write("<hr/>");            function test6(c){//函数要求传参                document.write("null和undefined<br/>");                var a = null;                var b;                document.write("a="+a);                document.write("<br/>类型  "+typeof(a));                document.write("<br/>b="+b);                document.write("<br/>类型  "+typeof(b));            }            test6();//调用的时候并没有传参,但依然可以调用        </script>    </head>    <body>          </body></html>
0 0
原创粉丝点击