+ 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 }

14th Jul 2018, 7:29 PM
Flo
8 Respostas
+ 2
Nguyễn Văn HoĂ ng Sorry, I just corrected it, now it's correct
15th Jul 2018, 1:35 PM
Dlite
Dlite - avatar
+ 1
and what is the benefit of 3?
14th Jul 2018, 8:08 PM
Flo
+ 1
so there is no benefit using 3 over 2 notation?
15th Jul 2018, 8:40 AM
Flo
+ 1
Yahiko Does the 2nd one with a = really work? I never encountered that kind of syntax before.
15th Jul 2018, 9:06 AM
HoĂ ng Nguyễn Văn
HoĂ ng Nguyễn Văn - avatar
0
It's supposed to be ================ 1. obj ={attribute: param} 2. function obj(param){ this.attribute = param } 3. var obj(param) =>{ this.attribute = param; }
14th Jul 2018, 7:58 PM
Dlite
Dlite - avatar
0
hans I rarely use it ,so I don't know
14th Jul 2018, 10:11 PM
Dlite
Dlite - avatar
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.
15th Jul 2018, 1:22 AM
HoĂ ng Nguyễn Văn
HoĂ ng Nguyễn Văn - avatar
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.
15th Jul 2018, 8:55 AM
HoĂ ng Nguyễn Văn
HoĂ ng Nguyễn Văn - avatar