Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSahil Prajapati
8,524 PointsSyntaxError: Unexpected token `{` while having a hash in the constructor.
I was trying to use a hash in the constructor as shown in the video to checkout destructuring but it doesn't work. Here is my js code:
"use strict";
class Cat {
constructor({name, height, color='orange'}={}) {
this.name = name;
this.color = color;
this.height = height;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
class Lion extends Cat {
speak() {
super.speak();
console.log(this.name + ' roars.');
}
}
let lion = new Lion({name:'Simba',height:2});
lion.speak();
This is the error that I get in my console:
/Users/sahil/Desktop/n.js:3
constructor({name, height, color='orange'}={}) {
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
1 Answer
andren
28,558 PointsEdit: I realize in hindsight that I was partially wrong about this comment, ignore anything stated below.
When you do destructuring in the parameters of a function/constructor you don't need to set it equal to something, just specify the properties you want to pull out and they will automatically be pulled out from the argument supplied to the function. So to fix your code just remove the "={}" like this:
"use strict";
class Cat {
constructor({name, height, color='orange'}) {
this.name = name;
this.color = color;
this.height = height;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
class Lion extends Cat {
speak() {
super.speak();
console.log(this.name + ' roars.');
}
}
let lion = new Lion({name:'Simba',height:2});
lion.speak();
And it will work as intended.
Sahil Prajapati
8,524 PointsSahil Prajapati
8,524 PointsThanks andren, it is still not working is it because of the node version
(v5.5.0)
that I have?andren
28,558 Pointsandren
28,558 PointsSahil Prajapati: Yes that is the issue. According to this website which tracks how compatible the various Node releases are with ES2015, Node 5.x is only compatible with about 59% of the features. And destructuring is one of the things not supported at all.
Is there some specific reason why you have to use version 5? The current LTS release is version 6.9.4 and that supports 99% of the ES2015 features, including all kinds of destructuring operations.
Edit:
I also have to add that I have realized in hindsight that my previous comment is actually incorrect, while it's true that you don't need to set the object equal to something it's also not invalid to do so. You can set it equal to an object that will be used as a default if no object is actually passed in as an argument. I have not played around with ES2015 features in a while so I momentarily forgot about that aspect of parameter destructing. If you upgrade to a version of Node that supports destructuring then your code should run fine as is without any changes.
Sorry for misinforming you with my previous comment. Since you were having issues running your code I assumed there had to be some mistake in it, and the equal bit was the thing that caught my eye as looking the most out of place, but I was mistaken about it being invalid.
Sahil Prajapati
8,524 PointsSahil Prajapati
8,524 Pointsandren I had installed node when I was using El Capitan, now just to update node I also have to update XCode. Thanks for all your help I am sure once the installation is complete it will start working.