如何测试javascript代码?

由于JavaScript程序不能像C++、Python程序等单独运行,不利于测试一段代码的功能,那么我们该如何车上JavaScript代码呢?下面我们就通过一个小例子来看一下如何测试JavaScript代码。

简单测试一小段JavaScript代码:

步骤

(1)新建一个test.html文件

(2)在test.html文件中书写以下内容

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script type="text/javascript">
</script>
</head>
<body>
</body>
</html>

(3)在test.html中编写需要测试的JS代码,如下

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script type="text/javascript">
 
function Student(name, age){
	this.name = name;
	this.age = age;
	this.description = function(){
		return this.name + "的年龄是:" + this.age;
	}
}
 
var p4 = new Student("Tony", 28);
var p5 = new Student("Tom", 40);
console.log(p4.description())	//输出为:Tony的年龄是:28
console.log(p5.description())	//输出为:Tom的年龄是:40
 
</script>
</head>
<body>
</body>
</html>

(4)再用浏览器打开test.html,按F12,查看console.log()打印输出的内容

总结:

JavaScript代码测试:需要将JavaScript代码嵌入HTML网页中,然后通过浏览器打开HTML文件,按F12快捷键进入浏览器的控制台查看JavaScript输出效果。

想要了解更多前端知识,可访问 前端开发学习!!

以上就是如何测试javascript代码?的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 答疑