Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

javascript - Object.defineProperty alternative for IE8

Given javascript code like the following (extracted from a plugin referenced below):

var AutosizeInput = (function () {
    function AutosizeInput(input, options) {
        var _this = this;
        this._input = $(input);
        this._options = options;


    }
    Object.defineProperty(AutosizeInput.prototype, "options", {
        get: function () {
            return this._options;
        },
        enumerable: true,
        configurable: true
    });
}

Full code of the plugin located at: https://github.com/MartinF/jQuery.Autosize.Input/blob/master/jquery.autosize.input.js

From what I read the call to Object.defineProperty will not work on IE8 as this is not a DOM object.

Is that accurate?..and if it is...which would be the best way to rewrite this getters (and setters) to be IE8 compliant?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

IE8 does not support getter/setter functions on properties of non DOM objects.

The "solution" here is to not rely on property getters, and use a full getter function instead.

AutosizeInput.prototype.getOptions = function() {
  return this._options;
};

var input = new AutoresizeInput();
input.getOptions();

Or, instead of keeping this._options as an "internal" variable, just drop the underscore allow access directly. This way you need no magic at all.

var AutoresizeInput = function() {
  this.options = {};
}

var input = new AutoresizeInput();
input.options();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...