0

I want to refer to an object within the object itself. The following is not right, i know. But what is the right way?

And when googling for such problems, what would be the right keywords to search for?

for (var key in eName) {
       var obj = eName[key];
       eName[key] = {
        type: 'id',
        name: obj.substr(1),
        html: this.type + '="' + this.name +'"'  //<--- here

       }
    }
4
  • Could you clarify what you are trying to achieve? What do you need this for/what problem are you trying to solve? Commented Jan 18, 2011 at 12:37
  • html: this.type + '="' + obj.substr(1) +'"' :) Commented Jan 18, 2011 at 12:38
  • Hmm. Are you referring to "this" when "this" isn't created yet...? So "this" is actually that - The "this" from the outer scope function... Commented Jan 18, 2011 at 12:45
  • 1
    when commenting on someone else comment use @ like I did here to notify him/her otherwise you won't get any response. first three letters are enough assuming there isn't anyone else with name starting in those letters. Commented Jan 18, 2011 at 12:48

4 Answers 4

2

Try using the JS equivalent for class instead:

for (var key in eName) {
    var obj = eName[key];
    eName[key] = new CustomElement(obj);
}

...

function CustomElement(strData) {
    this.type = "id";
    this.name = strData.substr(1);
    this.html = this.type + '="' + this.name +'"';
}
Sign up to request clarification or add additional context in comments.

Comments

1

The this keyword for Javascript might help you understand what this really means. You might have to pass it in as an object to the function.

Comments

0

Try this:

for (var key in eName) {
       var obj = eName[key];
       eName[key] = {
        type: 'id',
        name: obj.substr(1),
        html: ''  //<--- here

       }
eName[key].html = eName[key].type + '="' + eName[key].name +'"'

    }

Comments

0
for (var key in eName) {
       var obj = eName[key];
       eName[key] = {
        type: 'id',
        name: obj.substr(1),
        html: function() { return this.type + '="' + this.name +'"' }

       }
    }

Then you would use eName[key].html()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.