Что касается JS-новичка, я борюсь с JS-закрытием. Достаточно Google, хотя не могу понять, почему «это» недоступно в функции «store». Любая помощь будет оценена:JS: проблема закрытия Javascript в экземпляре прототипа
;(function (win) {
var _af = function(storeObjectName, storeObjectValue) {
var self;
if (_af.prototype.instance) {
self = _af.prototype.instance;
} else {
self = Object.create(_af.prototype);
_af.prototype.instance = self;
self._init();
}
if (arguments.length == 1) return self.fire(storeObjectName);
if (arguments.length == 2) self.store(storeObjectName, storeObjectValue);
return self;
};
_af.prototype = {
afVariable: '_af',
afObject: {},
_init : function(){
this.afObject = this.get(self.afVariable);
},
store : (storeObjectName, storeObjectValue)=>{
// This throws the error, that this.get is not defined
this.get('_af');
},
get : storageObject=>{
if (!storageObject)
this.afObject = '_af';
else
this.afObject = '_someother'
}
}
win._af = _af;
}(window));
Как вы создаете '_af'? –
Каков твой тест? Вы называете 'new _af', а затем запускаете' .store() '? Или вы просто пытаетесь '_af.store()'? – somethinghere
Связанная [Функция стрелки против объявления/выражения функции: эквивалентны ли они/заменяемы?] (Http://stackoverflow.com/questions/34361379/arrow-function-vs-function-declaration-expressions-are-they-equivalent-exch) –