IE8的Object.defineProperty替代方案 [英] Object.defineProperty alternative for IE8
问题描述
给出如下的javascript代码(从下面引用的插件中提取):
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
});
}
该插件的完整代码位于: https:/ /github.com/MartinF/jQuery.Autosize.Input/blob/master/jquery.autosize.input.js
Full code of the plugin located at: https://github.com/MartinF/jQuery.Autosize.Input/blob/master/jquery.autosize.input.js
从我读到的电话到Object.defineProperty不适用于IE8,因为它不是DOM对象。
From what I read the call to Object.defineProperty will not work on IE8 as this is not a DOM object.
这是准确的吗?..如果它是......将这个getter(和setter)重写为IE8兼容的最佳方法是什么?
Is that accurate?..and if it is...which would be the best way to rewrite this getters (and setters) to be IE8 compliant?
推荐答案
IE8不支持getter / setter关于非DOM对象属性的函数。
IE8 does not support getter/setter functions on properties of non DOM objects.
这里的解决方案是不依赖于属性getter,而是使用完整的getter函数。
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();
或者,而不是保持 this._options
作为内部变量,只需删除下划线即可直接访问。这样你根本不需要任何魔法。
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();
这篇关于IE8的Object.defineProperty替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!