简单测试JS代码

版权声明:转载请注明出处 https://blog.csdn.net/nk1212582/article/details/81321202

版权声明:转载请注明出处 https://blog.csdn.net/nk1212582/article/details/81321202

背景

由于JS程序不能像C++、Python程序等单独运行,不利于测试一段代码的功能

目的

简单测试一小段JS代码

步骤

(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()打印输出的内容

猜你喜欢

转载自blog.csdn.net/nk1212582/article/details/81321202