Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8695705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:06:03+00:00 2026-06-13T01:06:03+00:00

I have been trying all day to find a way to make this work.

  • 0

I have been trying all day to find a way to make this work. It’s really bugging me. I’ve read loads of articles on OOP and although i know my code is not perfect, I can’t seem to get around the initial problem that I have outlined in my code below.

All of the code works except the dynamic getter/setter – If you read my comments in the code or run the code you can easily see why.

I have a feeling what I’m trying to do can’t be done. Can someone tell me where I’m going wrong, or if It can’t be done the way i have proposed.

            var ext = {
        db: {
            string: function(def){
                def=def || {};
                def.type = "string";
                return def;
            },
            autoId: function(def){
                def=def || {};
                def.defaultvalue = function(){
                    return Math.uuid(def.length, def.radix)
                    return def;
                }
            }
        },
    }
    /*
     * @ext.base:       base class I can build out with common methods
     *                  
     */
    ext.base = function(){
        /*
         * place holder for the raw object data - 
         * 
         * {a:"martin webb",b:"is struggling with this"
         * 
         */
        this._rawdata={}; // this can later be strigified for storage; 

    };
    ext.base.prototype ={
        init:function(o){
            // pass in rawdata;
            this.load(o);
        },
        put:function(){
            //code removed
        },
        load: function(rawdata){
            this._rawdata=rawdata || {};
        }

    }
    /*
     * @ext.kind:       builds a new object we can use to make entities from our kind
     *                  martin=new people();
     * 
     */
    ext.db.kind=function(o){
        this._kind=function(o){this.init(o);} //this will pass any passed in object in the base class at init;
        this._kind.prototype = ext.base.prototype;

        var self=this;

        var prop,i=97; //ascii for "a";
        for (prop in o){
            if (o.hasOwnProperty(prop)) {
                /*
                 * This is like a dynamic getter setter, we are adding it to the prototype;
                 * 
                 * if you run the code when the function below is called
                 * value="martin webb" I need the var i to equal 97, and prop to = "name"
                 * in the function. I need a way to pass in and conserver these dynamic values
                 */
                this._kind.prototype[prop] = 
                    function(value){
                        if (value) {
                            self.rawdata[i] = value; //ERROR i=99!
                        }
                        return self.rawdata[i] ; //get the correct data using the key 
                    };

                    i++;
                }

        }
        return this._kind; // return our new object so we can use it like var martin=new people();
    }
    debugger;
    //lets make a new kind;
    var people={};
    people.name=ext.db.string();
    people.info=ext.db.string();
    people = new ext.db.kind(people);
    //and now make some entities usinhg it;
    var martin=new people();


    //THE CODE WILL GO WRONG HERE, THE SETTER WILL NOT WORK (SEE ABOVE)

    martin.name("martin webb")
    martin.info("is struggling with this code")

    var mike = new people({
        a: "mike",
        b: "has the solution"
    });
    console.log(mike.name()) //--> mike
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:06:04+00:00Added an answer on June 13, 2026 at 1:06 am

    I have found the solution. The answer was to use an enclosure and trap the loop index (i).

    This was beautifully explained in the article by

    Explaining javascript Scope and closures

    The alteration to my code is as follows and works a treat!

               var prop, i = 97; //ascii for "a";
                //loop each property and make our own get/setter that works
                //off our underlying compressed rawdata;
                for (prop in o) {
                    if (o.hasOwnProperty(prop)) {
                        /*
                         * this is the trick
                         * use an enclosure and pass our loop index in the bottom
                         * with a self calling function, this locks the value of i;
                         */
                        this._kind.prototype[prop] = function(k){
                            return function(value){
                                var key=String.fromCharCode(k);
                                //if we have a value passed in set it!
                                if (value){
                                    this._rawdata[key]=value;
                                }
                                //always return our value;
                                return this._rawdata[key];
                            };
                          }(i); //here we call our function and lock index i;
    
                        i++;
                    }//end loop;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying all day to get this to work and have been
I have been trying to find out the problem to this for a day
I have been trying all day to get app-engine sdk to work nothing has
I have been trying to solve this problem all day but haven't got any
I've been trying all day and just can't make the the TTPickerTextField work. It
Been trying to figure this one out all day. I have a large text
I have been trying to figure this out all day. I've searched SO and
I have been trying this all day now, and I am about ready to
I've been banging my head all day trying to fix this. I have a
I have been trying to do this all day. At the end i managed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.