I completely agree with the earlier feedback provided by other users. It's important to remember to call the getter-function in order to retrieve the "json" value.
You may want to consider declaring the json variable as well, unless it needs to be accessed globally in some way.
It appears that you are combining various object construction patterns. One option is the functional pattern, which creates "private" variables within a closure (such as `json`), while also utilizing prototypical inheritance to attach getters/setters to `this` inside the constructor function. Consider sticking to one approach for consistency.
For example: encapsulating privates within a closure.
function _bd() {
var json;
var that = {};
that.setJson = function(js) {
json = js;
}
that.getJson = function() {
return json;
}
return that;
}
var bd = _bd();
$.get("json.php", function(data) {
bd.setJson(data);
alert(bd.getJson());
},"json");
Another approach could be an Object-Oriented style using constructor functions.
function BD(){
this._json = null;
}
BD.prototype = {
getJson: function(){
return this._json;
},
setJson: function(json){
this._json = json;
}
};
var bd = new BD();
$.get("json.php", function(data) {
bd.setJson(data);
alert(bd.getJson());
},"json");
While there may be valid reasons for using a hybrid style, consistency between approaches can help streamline your code.
If you're interested in implementing "real" getters (despite the complex syntax), you can try:
function BD(){
this._json = null;
}
Object.defineProperty(BD.prototype,"json",{
get: function(){
return this._json;
},
set: function(json){
this._json = json;
}
});
var bd = new BD();
bd.json = {a: "test"};
console.log(bd);