[英]Simple console.log not working in javascript
我正在尝试运行一个非常简单的console.log("Hello world");
看看一切是否与 MS Code 和实时服务器一起工作,但我无法实现。
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<scirpt src="index.js"></scirpt>
</head>
<body>
</body>
</html>
然后,与 .html 位于同一目录中的 js 代码(index.js):
JavaScript
function Person(name){
name;
sayHello=function(){
console.log("Hello"+name);
}
}
let m=new Person('Michael');
m.sayHello();
那么,为什么我在控制台上看不到任何输出?
您必须将函数 (sayHello) 作为属性附加到Person
。 您还错误地将script
拼写为scirpt
:
function Person(name){ this.name = name; this.sayHello=function(){ console.log("Hello "+name); } } let m=new Person('Michael'); m.sayHello();
<script src="index.js"></script>
问题在于如何将属性分配给对象,方法是使用 this.key = value
function Person(name) { this.name = name; this.sayHello = function() { console.log("Hello " + name); } } let m = new Person('Michael'); m.sayHello();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.