【解決方法】データ構造 – "これ" キーワード

プログラミングQA


こんにちは、みんな、

いくつかの課題を解決していますが、このタスクを解決する方法がわかりません。

let classroom = {
    numOfStudents: 0,
    schoolHours: false,
    playtime: false,
    openSchool: function() {
        this.schoolHours = true;
        this.numOfStudents = 20;

*Inside the breakTime function, and using the this keyword where necessary, check if the key schoolHours is true, and if it is, set the key playtime to true

What I have tried:

let classroom = {
    numOfStudents: 0,
    schoolHours: false,
    playtime: false,
    openSchool: function() {
        this.schoolHours = true;
        this.numOfStudents = 20;
    },
    breakTime: function() {
       
           
     this.playTime= true;   
       
    }
};


classroom.openSchool(); 
console.log(classroom.numOfStudents);
classroom.breakTime();
console.log(classroom.playTime);

解決策 1

引用:

キー schoolHours が true かどうかを確認します

つまり、 if テスト。

解決策 2

リチャードが言ったことに付け加えると、 this 常にそれが使用されているクラスの現在のインスタンスを参照します。これにより、同じ名前のいくつかの変数のどれを使用するかを具体的にすることができます。 たとえば、と呼ばれるパラメーターを持つメソッドがある場合 foo クラスレベル変数とも呼ばれる foo、次にメソッド内でパラメーターが優先され、必要です this.foo クラスレベル変数にアクセスします。

ここを参照してください: このJavaScript[^]

コメント

タイトルとURLをコピーしました