Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Best way to accomplish the equivalent of "magic methods" for properties (Not methods)

es6 getter setter magic-methods overloading

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 jLinux

jLinux

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 30 November 2015 - 10:24 AM

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!



#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

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 jLinux

jLinux

    CC Lurker

  • New Member
  • Pip
  • 6 posts

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 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

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!'