+ 1
What's the difference between object notations?
hey I'm wondering why there are 3 different notations in JavaScript to define a object. var obj = { attribute : value } function obj(param){ attribute = param } var obj = (param) => { attribute = param }
8 Respostas
+ 2
Nguyá»
n VÄn HoĂ ng
Sorry, I just corrected it, now it's correct
+ 1
and what is the benefit of 3?
+ 1
so there is no benefit using 3 over 2 notation?
+ 1
Yahiko Does the 2nd one with a = really work? I never encountered that kind of syntax before.
0
It's supposed to be
================
1. obj ={attribute: param}
2. function obj(param){
this.attribute = param
}
3. var obj(param) =>{
this.attribute = param;
}
0
hans
I rarely use it ,so I don't know
0
1st defines an object.
2nd and 3rd declare a class. (Where 3rd is syntactic sugar version of 2nd introduced in EM6).
You have to use "new obj(arg)" to actually create an instance of the class.
0
hans the 2nd and 3rd don't create an object.
The other way to create an object outside literal notation is var obj = new Object().
People still prefer the literal version due to its convenience. It is also recommended by many IDE.
Edit: sorry was mistaking the question. No, there's no technical benefit using 3 over 2.