Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 呼叫班级';s集合方法的结果是;未捕获类型错误:session.issue不是函数;_Javascript - Fatal编程技术网

Javascript 呼叫班级';s集合方法的结果是;未捕获类型错误:session.issue不是函数;

Javascript 呼叫班级';s集合方法的结果是;未捕获类型错误:session.issue不是函数;,javascript,Javascript,我试图从类外部调用该会话类的set issue方法,结果导致未捕获的TypeError:Session.issue不是函数。有人能告诉我我做错了什么吗?我仍在排除故障,因此此代码中可能存在其他错误。当console.log记录对象本身时,在尝试访问其issue方法之前,它会返回如下结果: Session {_useSessionStorage: false, _useLocalStorage: false, _useServerStorage: false, _mostUpToDate: nul

我试图从类外部调用该会话类的set issue方法,结果导致未捕获的TypeError:Session.issue不是函数。有人能告诉我我做错了什么吗?我仍在排除故障,因此此代码中可能存在其他错误。当console.log记录对象本身时,在尝试访问其issue方法之前,它会返回如下结果:

Session {_useSessionStorage: false, _useLocalStorage: false, _useServerStorage: false, _mostUpToDate: null, _lastEdited: null, …}
_creation: 1602290317
_issue: null
_lastEdited: null
_lastOpened: null
_lines: null
_mostUpToDate: null
_useLocalStorage: false
_useServerStorage: false
_useSessionStorage: false
clearLines: (...)
creation: (...)
issue: (...)
lastEdited: (...)
lastOpened: (...)
lines: (...)
mostUpToDate: (...)
__proto__:
clearLines: (...)
constructor: class Session
creation: (...)
issue: (...)
lastEdited: (...)
lastOpened: (...)
lines: (...)
mostUpToDate: (...)
pullLocalData: ƒ pullLocalData()
pullSessionData: ƒ pullSessionData()
setLocalData: ƒ setLocalData(issue, lastOpened, lastEdited)
setServerData: ƒ setServerData(issue, lastOpened, lastEdited)
setSessionData: ƒ setSessionData(issue, lastOpened, lastEdited)
_pullLinesFromLocalStorage: ƒ _pullLinesFromLocalStorage()
_pullLinesFromServerDB: ƒ _pullLinesFromServerDB()
_pullLinesFromSessionStorage: ƒ _pullLinesFromSessionStorage()
_saveToLocalStorage: ƒ _saveToLocalStorage()
_saveToSessionStorage: ƒ _saveToSessionStorage()
_setData: ƒ _setData(issue, lastOpened, lastEdited)
_update: ƒ _update()
get clearLines: ƒ clearLines()
get creation: ƒ creation()
get issue: ƒ issue()
set issue: ƒ issue(newIssue)
get lastEdited: ƒ lastEdited()
get lastOpened: ƒ lastOpened()
get lines: ƒ lines()
set lines: ƒ lines(lines)
get mostUpToDate: ƒ mostUpToDate()
__proto__: Object
下面是完整的课程:

class Session {
    _useSessionStorage = false;
    _useLocalStorage = false;
    _useServerStorage = false;
    _mostUpToDate = null;
    _lastEdited = null;
    _lastOpened = null;
    _issue = null;
    _creation = null;
    _lines = null;

    constructor(creation) { this._creation = creation; }
    get creation()        { return this._creation; }
    get lastEdited()      { return this._lastEdited; }
    get lastOpened()      { return this._lastOpened; }
    set issue(newIssue)   { this._issue = newIssue; this._update(); }
    get issue()           { return this._issue; }
    get mostUpToDate()    { return this._mostUpToDate; }
    get clearLines()      { this._lines = null; }

    set lines(lines) {
        if (lines != this._lines) {
            this._lines = lines;
            this._update();
        }
    }

    get lines() {
        console.log("lines()", this._creation);
        this._lastOpened = Math.floor(Date.now() / 1000);
        if (this._lines == []) {
            switch (this._mostUpToDate) {
                case "Session": this._pullLinesFromSessionStorage(); break;
                case "Local":   this._pullLinesFromLocalStorage();   break;
                case "Server":  this._pullLinesFromServerDB();       break;
            }
        }
        return this._lines;
    }

    //creation -> issue, lastOpened, lastEdited, lines
    pullSessionData() {
        const session = JSON.parse(sessionStorage.getItem(this._creation));
        this.setSessionData(session[0], session[1], session[2]);
    }

    pullLocalData() {
        const session = JSON.parse(localStorage.getItem(this._creation));
        this.setLocalData(session[0], session[1], session[2]);
    }

    setSessionData(issue, lastOpened, lastEdited) {
        this._useSessionData = true;
        if (this._lastEdited == null || lastEdited >= this._lastEdited) {
            this._mostUpToDate = "Session";
            this._setData(issue, lastEdited, lastOpened);
        }
    }

    setLocalData(issue, lastOpened, lastEdited) {
        this._useLocalStorage = true;
        if (this._lastEdited == null || lastEdited >= this._lastEdited) {
            this._mostUpToDate = "Local";
            this._setData(issue, lastEdited, lastOpened);
        }
    }

    setServerData(issue, lastOpened, lastEdited) {
        this._useServerStorage = true;
        if (this._lastEdited == null || lastEdited > this._lastEdited) {
            this._mostUpToDate = "Server";
            this._setData(issue, lastEdited, lastOpened);
        }
    }

    //issue, lastOpened, lastEdited, lines
    _setData(issue, lastOpened, lastEdited) {
        this._issue = issue;
        this._lastEdited = lastEdited;
        this._lastOpened = lastOpened;
    }

    _update() {
        this._lastEdited = Math.floor(Date.now() / 1000);
        if (this._useLocalStorage)   { this._saveToLocalStorage();   this._mostUpToDate = "Local"; }
        if (this._useSessionStorage) { this._saveToSessionStorage(); this._mostUpToDate = "Session"; }
    }

    _saveToSessionStorage() {
        sessionStorage.setItem(creation, JSON.stringify(this._issue, this._lastOpened, this._lastEdited, this._lines));
    }

    _saveToLocalStorage() {
        localStorage.setItem(creation, JSON.stringify(this._issue, this._lastOpened, this._lastEdited, this._lines));
    }

    _pullLinesFromSessionStorage() {
        this._lines = JSON.parse(sessionStorage.getItem(this._creation))[3];
    }

    _pullLinesFromLocalStorage() {
        this._lines = JSON.parse(localStorage.getItem(this._creation))[3];
    }

    _pullLinesFromServerDB() {
    }
}
提前谢谢

在本节课中

set issue(newIssue)   { this._issue = newIssue; this._update(); }
get issue()           { return this._issue; }
上述方法是getter和setter(用
get
set
表示)。 它们不应该像我们调用函数那样直接调用。当您为属性
session.issue
赋值时,将隐式调用它们。与
session.issue=“Sample issue”
类似,将调用setter方法


使用
session.issue
访问值时,会隐式调用getter。

感谢您的回复!这是有道理的。我把它叫做object.issue(newIssue)。