@KindUserの答えに拡張:
あなたは、いくつかのプライベート状態を保存するために、このクラスの任意の場所にクロージャを使用していません。したがって、メソッドをインスタンスにではなく、インスタンスにアタッチする必要があります。すべてのインスタンスがインスタンスごとに1つずつではなく1つの関数を共有するので、より経済的です。そして、JSエンジンはそれをより良く最適化することができます。
その後、あなたはcheckAvailability
で別のエラーを持っている:それはthis
インスタンスのプロパティであるため、numRooms
はthis.numRooms
として対処する必要があり、この名前の変数がありません。
そして1つはスタイルです。
return condition;
//or if you want to enforce a Boolean value,
//but your condition may return only a truthy/falsy value:
return Boolean(condition);
//sometimes also written as:
return !!(condition);
次へ:あなたは
if(condition){
return true;
}else{
return false;
}
のようなものを持っている場合は、これを簡略化することができます。コーディング基準に固執する。 JSでは、大文字で始まる変数/プロパティはクラス/コンストラクタを示すため、HotelName
、HiltonHotel
、WeiHotel
は誤解を招きます。
プロパティ名はhotelName
で、冗長で直感的です。あなたはHotel
を持っています。それはname
ですが、これは単なる意見です。
var firstName = 'Steven';
var lastName = 'Curry';
var fullName = firstName + ' ' + lastName;
function Hotel(name) {
this.name = name;
this.numRooms = 20;
this.numGuests;
}
Hotel.prototype.checkAvailability = function() {
return this.numRooms !== 20;
}
Hotel.prototype.getHotelName = function() {
return this.name;
}
var hotel = new Hotel('Hilton');
var hName = document.getElementById('hotelName');
hName.textContent = hotel.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>
又はES6クラス(および一部の周りplayinの)として:
class Person{
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
//this is a getter, you can read it like a property
get fullName(){
return this.firstName + " " + this.lastName;
}
//this function is implicitely called whenever you try to convert
//an instance of `Person` into a string.
toString(){
return this.fullName;
}
}
class Hotel{
constructor(name) {
this.name = name;
this.numRooms = 20;
this.numGuests;
}
checkAvailability() {
return this.numRooms !== 20;
}
getHotelName() {
return this.name;
}
}
var steve = new Person('Steven', 'Curry');
var hotel = new Hotel('Hilton');
var hName = document.getElementById('hotelName');
hName.textContent = hotel.getHotelName(); // refer to the `weiHotel` variable
var el = document.getElementById('name');
el.textContent = steve.fullName;
//this uses the `toString()` method to convert the `Person` steve into a string
//for people, this makes sense, for the Hotel you'd want to think:
// - where do I want to use this?
// - and what should this string contain?
console.log("Hello, I'm " + steve + " and I'm at the "+ hotel.name);
<div id='greeting'> Hello
<span id="name">friend</span>!
<h1>Welcome To the <span id='hotelName'>Hyatt</span></h1>
</div>
'this.checkAvailabilityは{'有効な構文ではありません。あなたはおそらくより簡単なものから始めるべきです。 – Ryan
インスタンスからプロパティにアクセスする必要があります! 'HiltonHotel.getHotelName()'など。あなたの 'checkAvailability'メソッド(構文が無効です)は意味をなさない。部屋が20部屋ある場合はまだ利用可能です...ここで質問する前に、コンソールでエラーを確認してください – Li357
将来的に具体的にしてください。 * "うまくいかない" *私たちにはあまり言いません。エラーが発生している場合は、どの行にエラーが表示されますか?何が起こるか? –