Basic principles of properties

Hi!
I’m deeply pleased with Cocos Creator. This is what I was waiting for.
I’m a bit confused about properties. More precisely about getters/setters.
Could someone clarify it for me?

Let’s say I’d like custom getter and setter for a property. So I declare it like it is described in the documentation:
properties: { foo: { get: function () { return this._foo }, set: function… } }
Then should I add _foo to the list?
And how can I give it a default value?
And so can I return an initialised object’s method’s return value ( eg. return this._foo.getFoo() )?
Does it become a private member only if I declare the getter only?

These are the questions in my mind.
Thanks for your help in advance.

1 Like

Yes, take a look at the cc.Sprite implementation you will understand

https://github.com/cocos-creator/engine/blob/master/cocos2d/core/components/CCSprite.js#L129

Just set default in the property declaration, e.g.: https://github.com/cocos-creator/engine/blob/master/cocos2d/core/components/CCSprite.js#L142

You can set a default value like this:

 { foo: { default: obj.getFoo() } }

But you won’t have access to this here and you can’t use a function as default value.

Kind of, you won’t be able to set its value directly

Thanks for your answer.
The cc.Sprite example made it clear.
I have no problems with the properties since then.