Question

In the following code xFoo will be an object that (or its prototype) has an actual property bar with a value of 5 and it will will have an actual method foo(). What kind of object notation is this? This is not how I define properties in ECMAScript 5. I would have expected that xFoo.bar is an object that has a function get() and that xFoo.foo is an object that has a method value(). What am I missing here?

var XFoo = document.registerElement('x-foo', {
    prototype: Object.create(HTMLElement.prototype, {
        bar: {
            get: function () {
                return 5;
            }
        },
        foo: {
            value: function () {
                alert('foo() called');
            }
        }
    })
});

var xFoo = new XFoo();
Était-ce utile?

La solution

Yes, my guess was correct:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Parameters

Sorry, but have you at least tried to google javascript Object.create?

Autres conseils

What kind of object notation is this? This is not how I define properties in ECMAScript 5.

Of course it is. Just have a look at the docs of ES 5 Object.create. It uses the same property descriptors as Object.defineProperties does.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top