0

hasOwnProperty javascript メソッドで IE9+ および/または YUI 3 のバグを発見したと思います。ここにいる誰かが以前にこの問題を見たことがあるかどうか、または問題をさらに特定できるかどうか疑問に思っています。この問題について関係者に知らせたいのですが、IE チームと YUI のどちらに連絡する必要があるかわかりません。

詳細:

IE9/IE10の場合:

  • 標準モードでのレンダリング
  • 初期化された YUI 属性が含まれます (サンプル コードの 11 行目)。

hasOwnProperty は、有効な JavaScript 識別子ではないキーを含むオブジェクトに対して誤った値を返します (ab の 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 が返されます。

4

1 に答える 1

0

これは非常に奇妙な動作です。原因を突き止めるにはもう少し調査が必要ですが、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 {};
    }
  }
};
于 2013-04-16T15:02:03.893 に答える