JavaScriptの親クラスと子クラスに同じ名前のメソッドを含めることはできますか?
はい、親クラスと子クラスは同じ名前のメソッドを持つことができます。
例
class Parent {
constructor(parentValue) {
this.parentValue = parentValue;
}
//Parent class method name which is same as Child Class method name.
showValues() {
console.log("The parent method is called.....");
console.log("the value is="+this.parentValue);
}
}
class Child extends Parent {
constructor(parentValue,childValue){
super(parentValue);
this.childValue = childValue;
}
//Child class method name which is same as Parent Class method name.
showValues() {
console.log("The child method is called.....");
console.log("The value is="+`${this.childValue}`);
}
}
var parentObject = new Parent(100);
parentObject.showValues();
var childObject = new Child(400,500);
childObject.showValues(); 上記のプログラムを実行するには、次のコマンドを使用する必要があります-
node fileName.js.
ここで、私のファイル名はdemo195.jsです。
出力
これにより、次の出力が生成されます-
PS C:\Users\Amit\javascript-code> node demo195.js The parent method is called..... the value is=100 The child method is called..... The value is=500>
-
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> <style> &
-
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> <style> &nbs