【问题标题】:Javascript Object.prototype.hasOwnProperty returning incorrect value for IE9+ when using YUI attribute default valueJavascript Object.prototype.hasOwnProperty 在使用 YUI 属性默认值时为 IE9+ 返回不正确的值
【发布时间】:2013-04-11 23:43:04
【问题描述】:

我相信我在 IE9+ 和/或 YUI 3 中使用 hasOwnProperty javascript 方法发现了一个错误。我想知道这里是否有人以前见过这个问题,或者您是否能够进一步隔离问题。我想将此问题告知相关方,但我不知道是否需要联系 IE 团队或 YUI。

详情:

当IE9/IE10为:

  • 以标准模式呈现
  • 涉及初始化的 YUI 属性(示例代码中的第 11 行)

hasOwnProperty 为包含无效 javascript 标识符的键的对象返回不正确的值(a.b 中的 b 可以,a.5 中的 5 无效)。有效标识符没有这个问题。

示例代码:

http://jsfiddle.net/bobbyakadizzy/nLZJy/6/(记住这个问题只出现在IE9/IE10)

YUI.add("ie9-hasOwnProperty-test", function (Y) {

        function IE9HasOwnPropertyTest(config) {
            IE9HasOwnPropertyTest.superclass.constructor.apply(this, arguments);
        }

        Y.mix(IE9HasOwnPropertyTest, {
            NAME : "IE9HasOwnPropertyTest",
            ATTRS : {
                attributeWithNumericProperty : {
                    value : {}
                },
                attributeWithStringProperty : {
                    value : {}
                }
            }
        });

        Y.extend(IE9HasOwnPropertyTest, Y.Base, {

            initializer: function (config) {
                // Uncomment this section below the problem will be "fixed".  The problem appears to be related to attribute initialization.
                // this.set("attributeWithNumericProperty", {}); 
                var attributeWithNumericProperty = this.get("attributeWithNumericProperty");
                attributeWithNumericProperty['1234'] = 999;
                GLOBAL_attributeWithNumericProperty = attributeWithNumericProperty;

                // If the object property is an identifier can be set after a dot (e.g. a.b) then the problem will not be hit
                var attributeWithStringProperty = this.get("attributeWithStringProperty");
                attributeWithStringProperty['a1234'] = 999;
                GLOBAL_attributeWithStringProperty = attributeWithStringProperty;
            }
        });
        Y.IE9HasOwnPropertyTest = IE9HasOwnPropertyTest;
    }, "3.0.0", { requires : ["widget"]});

运行 new Y.IE9HasOwnPropertyTest(); GLOBAL_attributeWithNumericProperty.hasOwnProperty('1234'); 会在应该返回 true 时返回 false。

【问题讨论】:

  • 非常有趣!它似乎不是发生在“无效标识符”而是数字作为字符串。

标签: internet-explorer-9 yui internet-explorer-10 hasownproperty


【解决方案1】:

这是一种非常奇怪的行为。我需要做更多的研究来找出它发生的原因,但这似乎是 YUI 在默认值是对象时处理默认值的方式的结果。 YUI 尝试克隆对象以防止这种情况发生:

function MyClass() {
  MyClass.superclass.constructor.apply(this, arguments);
}
Y.extend(MyClass, Y.Base, null, {
  ATTRS: {
    foo: {
      value: []
    }
  }
});

var obj1 = new MyClass();
var obj2 = new MyClass();

obj1.get('foo').push('bar');
console.log(obj2.get('foo')); // ['bar'] oops!

您可以通过不使用对象作为默认值而是使用返回新对象的函数来修复它。这样你总是会得到一个新的对象,而 YUI 不会太聪明:

MyClass.ATTRS = {
  foo: {
    valueFn: function () {
      return {};
    }
  }
};

【讨论】:

    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2020-07-01
    • 1970-01-01
    • 2013-02-02
    • 2011-06-10
    • 2021-09-20
    相关资源
    最近更新 更多