简体   繁体   English

设置了JavaScript对象,但在访问时为null

[英]JavaScript Object is set but is null when accessed

I have the following object that constructs a session variable: 我有以下构造会话变量的对象:

var sessionObject = function (key) {
    this._key = key;
    this._content;
    this.set = function (v) {
        this.setLocal(v);
        $.post('/Program/SetVariable',
        { key: this._key, value: v }, function (data) {
        });
    };
    this.get = function (callback) {
        var setterCallback = this.setLocal;
        $.get('/Program/GetVariable',
        { key: this._key }, function (data) {
            setterCallback(data);
        }).done(function () {
            callback();
        });
    };
    this.setLocal = function (v) {
        this._content = v;
    };
    this.getLocal = function () {
        return this._content;
    };
}

And my C# in the controller is as follows: 我在控制器中的C#如下:

public ActionResult SetVariable(string key, string value)
{
    Session[key] = value;
    return this.Json(new { success = true });
}

public ActionResult GetVariable(string key)
{
    return this.Json(Session[key], JsonRequestBehavior.AllowGet);
}

I create a new session object every time the page is loaded, which references items in the session located on the server. 每当页面加载时,我都会创建一个新的会话对象,该对象引用服务器上会话中的项目。 When the session is set with the set() function, _content is set correctly and is able to be accessed publicly through item.getLocal() (either in the browser console or in code). 使用set()函数set()会话后, _content设置正确,并且可以通过item.getLocal() (在浏览器控制台或代码中)公开访问。

When I revisit the page and the session object referring to said item is already created, when I run the item.get() function it accesses the session variable and sets it to the _content object, I know this because I can do a console.log(this._content) in the setLocal() function which shows that the variable has been set correctly. 当我重新访问该页面并且已经创建了引用该项目的会话对象时,当我运行item.get()函数时,它将访问该会话变量并将其设置为_content对象,我知道这一点,因为我可以做一个console.log(this._content) setLocal()函数中的console.log(this._content) ,表明变量已正确设置。 But when I wish to access the content of the session object via either this.getLocal() or item._content while through the browser console or other lines of the code I get undefined returned to me. 但是,当我希望通过浏览器控制台或其他代码行通过item._content this.getLocal()item._content访问会话对象的内容时,我会得到未定义的信息。

So to illuminate the process some more this is what I do on a reload where there is already data in the session: 因此,为了进一步说明该过程,这是我在重新加载时执行的操作,其中会话中已有数据:

var item = new sessionObject("item");
item.get(printData);

function printData() {
   $("printbox").append(item.getLocal());
}

This does not print anything. 这不会打印任何内容。

Is there a reason I can not access this item's content unless it is specifically set by the item.set function? 除非是由item.set函数专门设置的,否则我无法访问此项目的内容吗?

Because you do this: 因为您这样做:

var setterCallback = this.setLocal;

and call it like so: 并这样称呼它:

setterCallback(data);

You have lost the context of your sessionObject instance, so the this inside the setLocal function is no longer your object instance but the global window object. 您丢失了sessionObject实例的上下文,因此setLocal函数中的this不再是您的对象实例,而是全局window对象。

You can do two things to correct this, save a reference to this instead of saving a reference to the function and call setLocal from that reference 你可以做两两件事来解决这个问题,保存到一个参考this ,而不是保存到函数的引用,并从引用调用SETLOCAL

var that = this;
/.../
that.setLocal(data);

or you can bind object instance when you save the setLocal reference 或者您可以在保存setLocal引用时绑定对象实例

var setterCallack = this.setLocal.bind(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM