KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
If I have a class that I pass a number of parameters to: class Foo constructor: (parameters) -> @bar = parameters.bar @moo = parameters.moo The class is created like so: foo = new Foo(bar: 2, moo: 8) My question is what is the most elegant way to detect in the constructor if the variables being passed exist, and if not to set a default. The way I would do it in javascript would be: this.bar = ( parameters.bar !== undefined ) ? parameters.bar : 10; where 10 is the default. Thanks for your help :) Good answers - Just to summerize the best: Inorder to detect if a parameter exists and define a default if it doesn't, in javascript is: this.bar = ( parameters.bar !== undefined ) ? parameters.bar : 10; and in coffescript is: @bar = parameters.bar ? 10 So elegant and compact!
Tags (comma-separated)
Save Edits
Cancel