gpt4 book ai didi

JavaScript 初学者 : why does this not work?

转载 作者:行者123 更新时间:2023-11-30 11:35:09 25 4
gpt4 key购买 nike

我的 html 页面没有响应我用 JS 编写的这段代码,我是一个初学者,刚开始学习 JS,有人能告诉我为什么这不起作用吗?

/* this is a practice file that'll play with js
nothing strange to look at here folks! */

var firstName = 'Steven';
var lastName = 'Curry';
var fullName = firstName + ' ' + lastName;

function Hotel(HotelName){
this.HotelName = HotelName;
this.numRooms = 20;
this.numGuests;
this.checkAvailability {
if(numRooms != 20 ){
return true;
}
else{
return false;
}
}
this.getHotelName = function(){
//can it work with this dot operator?
return this.HotelName;
}
}

var HiltonHotel = new Hotel('Hilton');
var hName = document.getElementById('hotelName');
hName.textContent = getHotelName();

var el = document.getElementById('name');
el.textContent = fullName;
<!DOCTYPE html>
<html>
<body>
<div id = 'greeting'> Hello
<span id="name">friend</span>!
<h1>Welcome To the <span id = 'hotelName'>Hyatt</span>
</div>
<script
src = "https://stacksnippets.net/js">
</script>
</body>
</html

我很确定它是有序的,我的语法需要改进,非常感谢任何建议,谢谢!

最佳答案

一些误解:

  • checkAvailability 是一个函数,您缺少括号。
  • 在访问 getHotelName 函数时,您必须引用 HiltonHotel 变量,才能访问和调用该函数。
  • html 代码中存在一些小错误,在代码片段 中运行时,您不必添加单独的脚本,默认情况下它是连接在一起的。

var firstName = 'Steven';
var lastName = 'Curry';
var fullName = firstName + ' ' + lastName;

function Hotel(HotelName) {
this.HotelName = HotelName;
this.numRooms = 20;
this.numGuests;
this.checkAvailability = function() { // it's a function (missing parens)
if (numRooms != 20) {
return true;
} else {
return false;
}
}
this.getHotelName = function() {
return this.HotelName;
}
}

var WeiHotel = new Hotel('Hilton');
var hName = document.getElementById('hotelName');
hName.textContent = WeiHotel.getHotelName(); // refer to the `WeiHotel` variable

var el = document.getElementById('name');
el.textContent = fullName;
<div id='greeting'> Hello
<span id="name">friend</span>!
<h1>Welcome To the <span id='hotelName'>Hyatt</span></h1>
</div>

关于JavaScript 初学者 : why does this not work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44790585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com