[英]C# get set javascript equivalent
javascript是否與以下C#代碼等效
public class Class
{
public string str
{
get;
set;
}
}
我正在嘗試做這樣的事情
var list = function () {
this.settings = {
id: 1
};
this.id = //something
}
var List = new list();
alert(List.id); //alerts 1
List.id = 5; // should set this.settings.id = 5
alert(List.settings.id); // alerts 5
盡管此答案對c#類有好處,但我仍然無法按如下方式引用ID
var list = function() {
this.settings = {
_id: 0,
get id() { return this._id; },
set id(x) { this._id = x; }
};
this.id = this.settings.id;
}
var List = new list();
document.write(List.id + "<br />");
List.id += 10;
document.write(List.settings.id + "<br />");
這可能是因為this.settings.id僅將值傳遞回this.id,而不是實際對象
該Wiki文章詳細介紹了Object.defineProperties,網址為http://en.wikipedia.org/wiki/Property_(programming)#JavaScript
是的, John Resig撰寫了很多文章 ,其基本思想是:
function Field(val){
var value = val;
this.__defineGetter__("value", function(){
return value;
});
this.__defineSetter__("value", function(val){
value = val;
});
}
范例: http : //jsfiddle.net/mu6jf/
var o = {
_p: 5,
get p() { return this._p; },
set p(x) { this._p = x; }
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.