开发者

What does `get` mean in an object literal?

开发者 https://www.devze.com 2023-03-02 03:59 出处:网络
Below is a snippet from Chrome Developer\'s tool: WebInspector.DOMS开发者_高级运维torage.prototype = {

Below is a snippet from Chrome Developer's tool:

WebInspector.DOMS开发者_高级运维torage.prototype = {
    get id()
    {
        return this._id;
    },

    get domain()
    {
        return this._domain;
    },

    get isLocalStorage()
    {
        return this._isLocalStorage;
    },

    getEntries: function(callback)
    {
        DOMStorageAgent.getDOMStorageEntries(this._id, callback);
    },

    setItem: function(key, value, callback)
    {
        DOMStorageAgent.setDOMStorageItem(this._id, key, value, callback);
    },

    removeItem: function(key, callback)
    {
        DOMStorageAgent.removeDOMStorageItem(this._id, key, callback);
    }
}

WebInspector.DOMStorage is a function and in the code above are its prototypes. The most strange for me is the following method: get id() or get something - I checked that into the object prototype only removeItem, getEntries and setItem are recognized. What about the others?


Those are getters. If you have an instance of DOMStorage you can do:

var domain = inst.domain;

but you can't assign to it (or you can but the value doesn't get changed):

inst.domain = 4; #doesnt change inst.domain

See this link for more info about it. Only some browsers support it.

0

精彩评论

暂无评论...
验证码 换一张
取 消