js基础

来源:互联网 发布:猫喜欢臭袜子 知乎 编辑:程序博客网 时间:2024/06/03 18:23
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>js类型</title>    <meta http-equiv="content-type" content="text/html; charset=UTF-8">  </head>  <body><script type="text/javascript">//num为number类型var num = 100;//str为string类型,注意js中的string类型用''或""均可var str = "哈哈";//flag为boolean类型var flag = true;</script><script type="text/javascript">//多个script块中的内容,可以相互访问//alert(flag);var person = null;var card;//alert(card);//undefined不是字符串,它是一种类型,如果你想判断某个变量是否为undefined,//通过如下代码判断: if(card == undefined){alert("card变量暂没值");  }else{ alert(card); }</script>  </body></html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>JS中有三种定义函数的方式</title>    <meta http-equiv="content-type" content="text/html; charset=UTF-8">  </head>  <body><script type="text/javascript">/*正常方式(先)function add(num1,num2){return num1 + num2;}window.alert("10+20=" + add(10,20));*/</script><script type="text/javascript">/*构造器方式,最后一个参数为函数体,每个参数都是字符串类型(后)var add = new Function("num1","num2","return num1+num2");window.alert("100+200=" + add(100,200));*/</script><script type="text/javascript">/*直接量或匿名或无名方式(再)var add = function(num1,num2){return num1 + num2;  }window.alert("1000+2000=" + add(1000,2000)); */ </script>  </body></html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>JS中有四种对象</title>    <meta http-equiv="content-type" content="text/html; charset=UTF-8">  </head>  <body>    <script type="text/javascript">//Date//var nowStr = new Date().toLocaleString();//window.document.write(nowStr + "<br/>");//Math//for(var i=1;i<=10;i++){////1到9之间的随机整数//document.write(Math.floor(Math.random()*9)+1 + "<br/>");//}//string//var str = "Hello你好";//var size = str.length; //alert(size);//7//Array//var array = new Array("语文","数学","英语",true,123);//for(var i=0;i<array.length;i++){//document.write(array[i] + "   ");//}</script><script type="text/javascript">/*自定义对象function Person(id,name,sal){this.id = id;this.name = name;this.sal = sal;}var p = new Person(1,"波波",7000);document.write("编号:" + p.id + "<br/>");document.write("姓名:" + p.name + "<br/>");document.write("薪水:" + p.sal + "<br/>");*/</script><script type="text/javascript">//window对象,打开新窗口//var url = "04_images.html";//window.open(url);</script><script type="text/javascript">//status对象,将当前时间设置到状态栏中//var nowStr = new Date().toLocaleString();//window.status = nowStr;</script><script type="text/javascript">//location对象,模拟用户在地址栏输入url访问其它页面的情况//var url = "04_images.html";//window.location.href = url;</script><script type="text/javascript">//history对象,演示刷新window.history.go(0);</script>  </body></html>



0 0
原创粉丝点击