I know, the topic sounds confusing, lol.
I'm getting into coding JS a lot more now, I usually do my projects in PHP, and some JS for the client side stuff, but recently, I've been doing a lot of JS projects, and I've been getting into ES6 as well (which is awesome!), and ill be switching one of my projects from PHP to Node/React.
One thing I'm currently working on, is a super easy way to implement a way to use private properties inside classes (which by the way, kinda blows me away thats not possible, at lest not traditionally, like in other languages). It seems like the best way to accomplish this, would be to use a WeakMap, and while I really hate having to do that (it just seems... not right), ill do it. But I would like to be able to create a way to use it in a more traditional sense.
I was hoping that there was something like the __noSuchMethod__, only for properties, not methods. Then I could do something fancy like check if it starts with an underscore, if so, add it to the WeakMap, then when trying to retrieve it, if it starts with an underscore, then retrieve it from the WeakMap. I know theres the Getters and Setters, but you have to know the names of the properties you're trying to get, if there was a way to execute a method for a property that doesn't exist, and hand down the name/value, that would be perfect.
So basically, what I'm looking for, is something thats the equivalent to PHP's overloading "magic" methods, __get and __set, which gets and sets properties that don't yet exist, (Not to be confused with the __call, which is for methods, not properties)
I know it's possible to do something like let _privVar = getPrivateVar('privVar');, but like I said, I'm looking for something thats a bit more "traditional".
Any help would be appreciated. Thanks!
Register and join over 40,000 other developers!
Recent Topics
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
-
Job Gig PHP Form Needed
PJohnson - Apr 18 2019 03:55 AM
-
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
-
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

Best way to accomplish the equivalent of "magic methods" for properties (Not methods)
Started by jLinux, Nov 30 2015 10:24 AM
es6 getter setter magic-methods overloading
3 replies to this topic
#1
Posted 30 November 2015 - 10:24 AM
#2
Posted 05 December 2015 - 08:03 PM
You could create an object with truly private variables inaccessible outside of the object like so:
function MyObject() { // Allow circumventing rebinding `this` so we always // get the same properties. var self = this; var myPrivateProperty = 'foo'; self.myPublicProperty = 'bar'; self.myPublicMethod = function (blah) { return '' + blah + self.myPublicProperty; }; }
sudo rm -rf / && echo $'Sanitize your inputs!'
#3
Posted 06 December 2015 - 05:25 PM
You could create an object with truly private variables inaccessible outside of the object like so:
function MyObject() { // Allow circumventing rebinding `this` so we always // get the same properties. var self = this; var myPrivateProperty = 'foo'; self.myPublicProperty = 'bar'; self.myPublicMethod = function (blah) { return '' + blah + self.myPublicProperty; }; }
Regarding the var self = this, ES6 has a lexical this for that.
And for the rest of this, I'm not just talking about single functions/methods, I'm talking about an entire class... Example here: http://jsfiddle.net/jLinux/ke0fg722/
#4
Posted 06 December 2015 - 06:39 PM
Well that is a class. You'd create it with instance = new MyObject().
sudo rm -rf / && echo $'Sanitize your inputs!'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download