admin管理员组文章数量:1026373
checkbox: (propertyName, {hash}) ->
...
...
What does this mean?
I'm familiar with the concept of
class Person
constructor: (name) ->
@name = name
having a shorthand of
class Person
constructor: (@name) ->
Does {parameterName} have similar magic?
checkbox: (propertyName, {hash}) ->
...
...
What does this mean?
I'm familiar with the concept of
class Person
constructor: (name) ->
@name = name
having a shorthand of
class Person
constructor: (@name) ->
Does {parameterName} have similar magic?
Share Improve this question asked Aug 8, 2013 at 17:19 Foolish ChapFoolish Chap 7651 gold badge5 silver badges19 bronze badges 04 Answers
Reset to default 6It's undocumented parameter destructuring
(propertyName, {hash}) ->
Is short for -->
(propertyName, obj) ->
hash = obj.hash
And this
(propertyName, {hash, something}) ->
Is short for -->
(propertyName, obj) ->
hash = obj.hash
something = obj.something
And so on. It works pretty much like the normal destructuring.
This
checkbox = (propertyName, {hash}) ->
piles directly to this in JS
var checkbox;
checkbox = function(propertyName, _arg) {
var hash;
hash = _arg.hash;
};
So it gets the property of an object being passed in and sets it as the top level variable name. Whether this is a good thing or not is up for debate, especially since it doesn't seem to be a documented language feature (that I can find)
Coffeescript's site has a helpful tool for investigating things like this: Try Coffeescript
When in doubt, I remend js2coffee to check out the rendered output. You can also use codepen to play around and find the results of certain actions. For example see this as a DEMO
foo = (bar, {baz}) ->
console.log bar+baz
opts =
qaz : 'hey'
baz : 'wowzers'
foo "go go", opts
# console will log "go go wowzers"
renders to ->
var foo, opts;
foo = function(bar, _arg) {
var baz;
baz = _arg.baz;
return console.log(bar + baz);
};
opts = {
qaz: 'hey',
baz: 'wowzers'
};
foo("go go", opts);
It makes it so you can directly use the options names instead of having to do options.property, ie
func = (someValue, {shouldDoSomething, doWork}) ->
# use option names directly
doWork someValue if shouldDoSomething
instead of
func = (someValue, options) ->
options.doWork someValue if options.shouldDoSomething
checkbox: (propertyName, {hash}) ->
...
...
What does this mean?
I'm familiar with the concept of
class Person
constructor: (name) ->
@name = name
having a shorthand of
class Person
constructor: (@name) ->
Does {parameterName} have similar magic?
checkbox: (propertyName, {hash}) ->
...
...
What does this mean?
I'm familiar with the concept of
class Person
constructor: (name) ->
@name = name
having a shorthand of
class Person
constructor: (@name) ->
Does {parameterName} have similar magic?
Share Improve this question asked Aug 8, 2013 at 17:19 Foolish ChapFoolish Chap 7651 gold badge5 silver badges19 bronze badges 04 Answers
Reset to default 6It's undocumented parameter destructuring
(propertyName, {hash}) ->
Is short for -->
(propertyName, obj) ->
hash = obj.hash
And this
(propertyName, {hash, something}) ->
Is short for -->
(propertyName, obj) ->
hash = obj.hash
something = obj.something
And so on. It works pretty much like the normal destructuring.
This
checkbox = (propertyName, {hash}) ->
piles directly to this in JS
var checkbox;
checkbox = function(propertyName, _arg) {
var hash;
hash = _arg.hash;
};
So it gets the property of an object being passed in and sets it as the top level variable name. Whether this is a good thing or not is up for debate, especially since it doesn't seem to be a documented language feature (that I can find)
Coffeescript's site has a helpful tool for investigating things like this: Try Coffeescript
When in doubt, I remend js2coffee to check out the rendered output. You can also use codepen to play around and find the results of certain actions. For example see this as a DEMO
foo = (bar, {baz}) ->
console.log bar+baz
opts =
qaz : 'hey'
baz : 'wowzers'
foo "go go", opts
# console will log "go go wowzers"
renders to ->
var foo, opts;
foo = function(bar, _arg) {
var baz;
baz = _arg.baz;
return console.log(bar + baz);
};
opts = {
qaz: 'hey',
baz: 'wowzers'
};
foo("go go", opts);
It makes it so you can directly use the options names instead of having to do options.property, ie
func = (someValue, {shouldDoSomething, doWork}) ->
# use option names directly
doWork someValue if shouldDoSomething
instead of
func = (someValue, options) ->
options.doWork someValue if options.shouldDoSomething
版权声明:本文标题:javascript - Coffeescript: what does it mean to have curly brackets around a method parameter? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745645908a2161024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论