MUstar

Javascript ES6 Example - Enhanced Object Literals

Aug 2nd, 2018
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var sales = "Toyota";
  2.  
  3. function carTypes(name) {
  4.     if (name === "Honda"){
  5.         return name;
  6.     }else{
  7.  
  8.     }
  9.  
  10.     return "Sorry, we don't sell " + name +  ".";
  11. }
  12.  
  13. var car = {mycar : 'Saturn' , getCar : carTypes('Honda'), special: sales};
  14.  
  15. console.log(car.mycar);
  16. console.log(car.getCar);
  17. console.log(car.special);
  18.  
  19.  
  20. var car2 = {manyCars : {a: 'Saab' , 'b': "Jeep"} , 7 : "Mazda"};
  21.  
  22. console.log(car2.manyCars.b);
  23. console.log(car2[7]);
  24.  
  25.  
  26.  
  27. var unusualPropertyNames = {
  28.     "" : "An empty string",
  29.     "!" : "Bang!"
  30. }
  31.  
  32. // console.log(unusualPropertyNames."");
  33. console.log(unusualPropertyNames[""]);
  34. // console.log(unusualPropertyNames.!);
  35. console.log(unusualPropertyNames["!"]);
  36.  
  37.  
  38. // var obj = {
  39. //     __proto__ : theProtoObj,
  40. //     handler,
  41. //     toString(){
  42. //         return "d " + super.toString();
  43. //     },
  44. //     ['prop_' + (() => 42)() ] : 42
  45. // };
  46.  
  47. var foo = {a : 'alpha' , 2: "two"};
  48. console.log(foo.a);
  49. console.log(foo[2]);
  50. // console.log(foo[a]);
  51. // console.log(foo.2);
  52. console.log(foo['a']);
  53. console.log(foo['2']);
  54. // console.log(foo.['a']);
  55. // console.log(foo.['2']);
RAW Paste Data