[RESOLVED] using predefined strings to assign to private members possible?
Results 1 to 7 of 7

Thread: [RESOLVED] using predefined strings to assign to private members possible?

  1. #1
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,657

    resolved [RESOLVED] using predefined strings to assign to private members possible?

    The part of the code I want help with is in the last part, starting with
    for (x in index) {
    The rest of the code is a minimized reference for clarification purposes.

    The question is wether I can use x to assign to private members. this[x] assigns to public members
    Code:
    /**
     * @param pd string planet data
     */
    function Planet(pd) {
    	var name, maxpop, type, notes, owner, pop, factories, bases, stargate;
    	/**
    	 * @member index contains indices into the pd string
    	 */
    	var index = {
    		'name'	: {
    			'start'	: 0,
    			'end': 10
    		},
    		'maxpop'	: {
    			'start'	: 30,
    			'end'	: 31
    		},
    		'type'	: {
    			'start'	: 32,
    			'end'	: 33
    		},
    		'etc' : {}
    	}
    
    	/* privileged functions */
    	this.getName = function() { return name; }
    	/* If I used this.maxpop here, I could use this[x] below. But then the members are
    	 * also publicly accessible which they shouldn't be
    	 */
    	this.getMaxPop = function() { return maxpop; }
    	/* etc */
    
    	/* private functions */
    	function reverseByte(val) {
    		var total = 0;
    		for (var i = 0; i < val.length; ++i) {
    			total += parseInt(val.charCodeAt(i)) * Math.pow(256, (val.length - 1) - i);
    		}
    		return total;
    	}
    	
    	/*
    	 *	Is it possible to use the object property names from the "index" member above
    	 *	to assign to private members name, maxpop etc?
    	 *
    	 * using this[x] assigns to public members name, maxpop etc. That is, when x is "maxpop",
    	 * this[x] will not assign to the private member maxpop.
    	 *
    	 * And doing it the way I've done below seems cumbersome.
    	 */
    	for (x in index) {
    		if (x == 'name') {
    			name = pd.substring(index[x].start, index[x].end);
    		}
    		else {
    			switch(x) {
    				case 'maxpop':
    					maxpop = reverseByte(pd.substring(index[x].start, index[x].end));
    					break;
    				case 'type':
    					type = reverseByte(pd.substring(index[x].start, index[x].end));
    					break;
    				case 'etc':
    					...;
    					break;
    			}
    		}
    	}
    }
    Last edited by johanafm; 03-27-2013 at 02:37 PM.

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,715
    Gee, I hope you've asked this someplace else. Aren't you the JS guru around here?
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,884
    A set of accessor functions of some sort?
    Code:
    function Planet() {
    ...
        this.set = {maxpop: function(val) { maxpop = val; }};
        this.get = {maxpop: function(){return maxpop;}};
    ...
    }
    
    
    planetInstance.set.maxpop(42);
    var foo = planetInstance.get.maxpop();
    planetInstance.set['maxpop'](17);
    Last edited by Weedpacket; 03-27-2013 at 08:52 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #4
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,657
    Quote Originally Posted by dalecosp View Post
    Gee, I hope you've asked this someplace else. Aren't you the JS guru around here?
    Well, I started this little project to learn (more) javascript

    I was actually certain that Weedpacket would have an answer to this... And voilą

    Quote Originally Posted by Weedpacket View Post
    A set of accessor functions of some sort?
    Code:
    function Planet() {
        this.set = {maxpop: function(val) { maxpop = val; }};
    Brilliant. A setter object handles this perfectly. And also, the same technique made it possible to do away with the conditional branch to call either getNullTerminated when setting Planet.name and reverseByte for the others!

    Thanks

    For reference, it now looks like this
    Code:
    	var indices = {
    		'name'	: {
    			'start'	: 0,
    			'end'	: 12,
    			'extractor'	: getNullTerminated
    		},
    		'maxpop'	: {
    			'start'	: 30,
    			'end'	: 31,
    			'extractor'	: reverseByte
    		},
    		etc : {}
    	}
    
    	this.set = {
    		name	: function (v) { name = v; },
    		maxpop	: function (v) { maxpop = v; },
    		etc : {}
    	}
    
    	// this.set[x] => this.set.name, this.set.maxpop
    	// and now also calling indices[x].exctractor for indices.name.getNullTerminated(), indices.maxpop.reverseByte()
    	for (x in index) {
    		this.set[x](indices[x].extractor(pd.substring(index[x].start, index[x].end)));
    	}
    And while it may perhaps initially look as if it's an equal amount of typing to get those setter functions in place as typing it out explicitly... First off, I need those setters anyway. Besides, now I can do away with the former setter functions which could not be called the way I needed them: Planet.setName(), Planet.setMaxPop() etc.
    Last edited by johanafm; 03-28-2013 at 07:55 AM.

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,715
    Quote Originally Posted by johanafm View Post
    Well, I started this little project to learn (more) javascript
    Bully for you! +1 for personal growth
    I was actually certain that Weedpacket would have an answer to this... And voilą
    Lol, no kidding. I just guess I don't assume he'll answer *everything*.

    Mr. Packet ... assuming there's a bonus this month (I heard rumors) ... do you have an Amazon wishlist? You're too many TZ's away for the lunch/beer/pizza thing ...
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,884
    Just as a follow-up, I recall that JavaScript does now have support for getter/setter properties (subject to availability in client engines).
    Code:
    get maxpop () {
      return this.privateMaxpop;
    }
    ....
    var t1 = o.maxpop;
    var t2 = o['maxpop'];
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  7. #7
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,657
    Thanks for the additional info Weedpacket. But I end up with another couple of questions. I'm starting a new topic for it though, since it's moving off from this one.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •