0
let name = "asraf";
var a = "hello";
var Obj = {
  [a+name] : "Asraful islam ",//it's working 
  course : "Btec",
  detail : function (){
    return this.a+name+ " is a student of " + this.course; // it's didn't working 
  }
}
console.log(Obj.detail());

output : undefinedasraf is a student of Btec

shorif
  • 11
  • 3
  • The [a+name] is not allowed!! – Hangover Sound Sound Feb 03 '22 at 16:36
  • @HangoverSoundSound, sure it is. Did you try? – trincot Feb 03 '22 at 16:39
  • Use the same syntax as you did to define the property: `return this[a+name]` – trincot Feb 03 '22 at 16:39
  • Very strange that this question was closed. Shorif, remove ``this.`` from ``this.a`` so its just ``a``, and you should not have any more problems. – Kenny.Harrer Feb 03 '22 at 16:39
  • @Kenny.Harrer, it is not strange that it was closed, since this has been asked many times before in different ways. And what you propose will just print the value of `a`, not the property in the object, which is what the Asker wants. – trincot Feb 03 '22 at 16:40
  • @Kenny.Harrer, I provided help above. Did you read my comment? And yes, this is duplicate. But still I provided the way out in comments. How is that not helping? – trincot Feb 03 '22 at 16:42
  • @trincot When I wrote my comment you had not commented yet. It's a misunderstanding. – Kenny.Harrer Feb 03 '22 at 16:45

0 Answers0