Reputation: 1067
The class gets instantiated and stored in a variable. The reference of the variable is used to change 'prop' to 'change'. The reference is passed and stored in another variable and logged out. Aaaaaaaaaannnndddd I don't get it.
function ctrl () {
function Klasse () {
this.prop = 'prop';
}
var _klasse = new Klasse();
return {
get: function () {
console.log('get', _klasse);
return _klasse
},
change: function () {
_klasse.prop = 'changed';
console.log('change', _klasse);
},
reset : function () {
_klasse = new Klasse();
}
}
}
ctrl().change();
var _klasseReferenz = ctrl().get();
// Why is _klasseReferenz.prop !== 'changed' ????
console.log('referenz ', _klasseReferenz);
Fiddle: http://jsfiddle.net/Lfg85x96/1/
Upvotes: 0
Views: 45
Reputation: 592
You are creating to references of the ctrl function. Instead of ctrl().change() and ctrl().get() just assign a new ctrl instance to a variable
var _ctrl = new ctrl();
ctrl.change();
ctrl.get();
Upvotes: 0
Reputation: 407
ctrl().change(); --> you are just calling function seperately (not saving reference call)
var _klasseReferenz = ctrl().get(); --> In this like you are creating an object.
// Why is _klasseReferenz.prop !== 'changed' ???? console.log('referenz ', _klasseReferenz);
In first line you are only calling function and there is no reference, from the second line onward you have created an object and start refrencing it.
Use below line of code to understand
var ctl = new ctrl();
ctl.change();
console.log('NowRefrenceWorking ', ctl.get());
Upvotes: 0
Reputation: 33399
Each time you call ctrl()
you create a new instance of _klass
local to that closure. You need to save the reference call, and call get and update on that.
var reference = ctrl();
reference.change();
var _klasseReferenz = reference.get();
Upvotes: 0
Reputation: 3938
Because every time you call ctrl
it will create new Klasse
instance. You should change this to:
var ctrlInstance = new ctrl();
ctrlInstance.change();
var _klasseReferenz = ctrlInstance.get();
console.log('referenz ', _klasseReferenz);
See fiddle
Upvotes: 1