![](/img/trans.png)
[英]Javascript Object Literal referring to another property in itself from another property
[英]How to access an object literal property from another property?
這可能非常簡單,我的Google-fu還不夠強大。 如果它是重復的,我道歉。
考慮以下對象文字:
var config = {
url: 'http://google.com',
message: 'You must go to <a href="' + url + '">Google</a> to search!'
};
我收到一個錯誤,說url is not defined
。 如何從消息元素中訪問url元素?
我想你可以包裝config
對象,例如
var config = (function() {
var _url = 'http://google.com';
return {
url : _url,
message : 'You must go to <a href="' + _url + '">Google</a> to search!'
}
})();
你可以這樣做
var config = {
url: 'http://google.com',
message: function () { return 'You must go to <a href="' + this.url + '">Google</a> to search!' }
};
並致電
config.message();
得到消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.