Javascript: previous property is undefined
Why is it saying that "lbp is undefined" on the line of "creditText"? How do I refer to previous properties in a config file such as this?
var lbp = {
// Pertinant page properties, such as Author, Keywords, URL or Title
page: {
theURL: window.location.toString(),
},
// Configurable user defaults
defaults: {
creditText: lbp.page.theURL
}
}
Thanks in advance开发者_开发百科 for your help
You don't. lbp won't exist in the current scope's symbol table until the object is closed out.
var lbp = {
// Pertinant page properties, such as Author, Keywords, URL or Title
page: {
theURL: window.location.toString(),
}
}; // NOW you can reference lbp by name
lbp.defaults = {
creditText: lbp.page.theURL
};
You just can't, your lbp variable is not defined since the last parenthesis of the declaration is closed.
I would guess that the contents of the object you are defining are being interpreted before the value is assigned to the lbp variable. I don't think there's any way to do what you want without assigning the values in a separate instruction.
var lbp = {};
// Pertinant page properties, such as Author, Keywords, URL or Title
lbp.page = { theURL: window.location.toString() };
// Configurable user defaults
lbp.defaults = { creditText: lbp.page.theURL };
精彩评论