You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.9 KiB
71 lines
1.9 KiB
2 years ago
|
# I'm confused with class `get` `set` and `prop`
|
||
|
|
||
|
We have 2 places in documentation describing it:
|
||
|
|
||
|
[A]
|
||
|
[https://imba.io/docs/basic-syntax/classes#computed-properties](https://imba.io/docs/basic-syntax/classes#computed-properties)
|
||
|
|
||
|
[B]
|
||
|
[https://imba.io/docs/basic-syntax/operators#keywords-set](https://imba.io/docs/basic-syntax/operators#keywords-set)
|
||
|
|
||
|
__[A]__ describes:
|
||
|
|
||
|
`get`
|
||
|
> The get syntax binds an object property to a function that will be
|
||
|
> called when that property is looked up
|
||
|
|
||
|
But there's no bind. If you call `get` the same as property you get into
|
||
|
infinite loop. If you call it differently - like in examples on website
|
||
|
which doesn't support the description clame -
|
||
|
then it works like a regular function and no difference between using
|
||
|
`get` or `def` other than self documenting syntax.
|
||
|
|
||
|
__1. What's the differenece between `get` and `def`?__
|
||
|
|
||
|
`set`
|
||
|
> Using set you can define a function that will be called when there is
|
||
|
> an attempt to set that property.
|
||
|
|
||
|
Sounds clear but then example doesn't show it and then it's not clear
|
||
|
until you check it. Proper example:
|
||
|
|
||
|
```coffee
|
||
|
class Rect
|
||
|
sides = 0
|
||
|
width = 0
|
||
|
height = 0
|
||
|
|
||
|
set sides size
|
||
|
width = size
|
||
|
height = size
|
||
|
```
|
||
|
|
||
|
I would expect different behaviour from `set` - method is called on
|
||
|
value change > process value > returns processed value > applies
|
||
|
returned value instead. Probably the thing which is proposed in
|
||
|
undocumented section as `$set` but with not practical syntax.
|
||
|
|
||
|
__2. I see no consistency between `get` and `set` as set binds on write
|
||
|
and get doesn't bind on lookup.__
|
||
|
|
||
|
No `prop` description or usage in any example of Class documentation.
|
||
|
|
||
|
__[B]__ describes `prop` and use it in examples:
|
||
|
|
||
|
`prop`
|
||
|
>A prop allows a tag property to be set from the outside
|
||
|
|
||
|
But you have the same behaviour without keyword prop.
|
||
|
|
||
|
__3. What's the difference between following?__
|
||
|
```coffee
|
||
|
class A
|
||
|
prop a = 1
|
||
|
|
||
|
# and
|
||
|
|
||
|
class B
|
||
|
b = 1
|
||
|
```
|
||
|
|