JavaScript-引入方式、数据类型

引入方式
  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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值