I’m evaluating selfish and am wondering how I can declare private methods/fields?
I’m evaluating selfish and am wondering how I can declare private methods/fields?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The usual way to do private functions is to use a function that your various “methods” close over, e.g. picking up their
Dogexample, change this:To
Usage:
Re private fields, the usual way is to build anything that needs truly private fields from within your constructor function so they close over the (private) variables within the call to the constructor, a’la Crockford’s pattern:
Usage:
…selfish does away with the actual constructor function, but the
initializefunction should serve the same purpose:Now, you can’t set the
woof(except at construction time, because we did that on purpose), you can only retrieve it frombark. E.g., it’s truly private (other than that we’ve explicitly allowedbarkto return it).If you get into the technical details of this, these articles from my blog may (or may not) be useful, since when you get into private functions and such, you typically have to start managing
this:thisAnd if you want another thing to evaluate, there’s my
Lineageproject, which is similarly pure prototypical inheritance, but with easy access to parent object properties, functions, etc. and a syntax that actively encourages private scopes for this sort of thing.