版权声明:转载请注明出处 https://blog.youkuaiyun.com/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()打印输出的内容