Reputation: 1160
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
}
}
Upvotes: 0
Views: 671
Reputation: 686
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()
Upvotes: 0
Reputation: 66396
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 +'"';
}
Upvotes: 2
Reputation: 7566
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 +'"'
}
Upvotes: 0
Reputation: 50038
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.
Upvotes: 1