特定のクラスのオブジェクトの値がJavaScriptで変更されているかどうかを確認し、それに基づいて別の値を更新しますか?
これを確認するには、getterの概念、つまりgetプロパティを使用します。以下はコードです-
例
class Student{
constructor(studentMarks1, studentMarks2){
this.studentMarks1 = studentMarks1
this.studentMarks2 = studentMarks2
var alteredValue = this;
this.getValues = {
get studentMarks1() {
return alteredValue.studentMarks1
},
get studentMarks2() {
return alteredValue.studentMarks2
}
}
}
}
var johnSmith = new Student(78,79)
console.log("Before incrementing the result is=")
console.log("StudentMarks1="+johnSmith.studentMarks1,"StudentMarks2="+johnSmith.studentMarks2);
johnSmith.studentMarks2+=10;
console.log("After incrementing the value 10 in the studentMarks2, the result is as follows=")
console.log("StudentMarks1="+johnSmith.studentMarks1,
"StudentMarks2="+johnSmith.getValues.studentMarks2); 上記のプログラムを実行するには、次のコマンドを使用する必要があります-
node fileName.js.
ここで、私のファイル名はdemo200.jsです。
出力
これにより、次の出力が生成されます-
PS C:\Users\Amit\javascript-code> node demo200.js Before incrementing the result is= StudentMarks1=78 StudentMarks2=79 After incrementing the value 10 in the studentMarks2, the result is as follows= StudentMarks1=78 StudentMarks2=89
-
CSSとJavaScriptを使用してクラス名に基づいてDIV要素をフィルタリングするにはどうすればよいですか?
クラス名に基づいてDIV要素をフィルタリングするには、コードは次のとおりです- 例 <!DOCTYPE html> <html> <style> .filterElements { float: left; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; font-size: 20px;
-
オブジェクトがJavaScriptのクラスのインスタンスであるかどうかを確認するにはどうすればよいですか?
以下は、オブジェクトがJavaScriptのクラスのインスタンスであるかどうかを確認するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title>