Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.defineProperty for all browsers?

Tags:

Asking about Object.defineProperty as demonstrated below:

function testComponent(){
    var testProperty;
    Object.defineProperty(this, "testProperty",
    {
        get : function()
        {
           return testProperty;
        },
        set : function(val)
        {
          testProperty = val;
        }
    });
}

Where it would be used like so:

testObject = new testComponent();
testObject.testProperty = "testValue";

Based on what I've seen so far, it looks like there is no cross browser solution, as I've tried using es5-shim with no luck, but I would like to confirm. I also found a reference to this post and my tests still fail in IE 7 & 8, can anyone shed any light on this?

I remember looking through a related question a few months ago somewhere on S/O and I think I saw someone had written a solution for this in an answer. Any general workarounds for getter / setters would also be appreciated. The idea is that I need some equivalent of a getter setter on an object without passing the parameter change through a method. I don't need IE6, but I would like to support browsers in the range of IE7+ ff 3.6+ , etc


QUnit tests below: (jsFiddles)

(these pass in all browsers on my machine except IE 7 & 8

direct use of defineProperty, no shims:
http://jsfiddle.net/uSYFE/

fiddle using the ES5 shim, I'm assuming all I need to do is include it? :
http://jsfiddle.net/hyperthalamus/ntwDy/

fiddle using the IE recommended solution :
http://jsfiddle.net/hyperthalamus/xfvz3/

like image 747
Shane Avatar asked Apr 23 '12 14:04

Shane


People also ask

What is object defineProperty ()?

defineProperty() The static method Object. defineProperty() defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

What parameters does the function object defineProperty () accept?

Object. defineProperty allows you to set whether or not the property is enumerable, writable, and configurable as well as a value or a get/set (getter/setter) pair (see MDN Object.

What is the difference between GET and defineProperty?

Using getter syntax you create a property which, prior to ES2015, you had to know the name of at the time that you were writing the code. Object. defineProperty allows you to perform the same as the above but, even before ES2015, does not require you to know the name of the property in advanced.

What is the syntax of object defineProperty method in JavaScript?

Syntax: Object. defineProperty(obj, prop, descriptor)


2 Answers

According to ES5-shim:

/!\ Object.defineProperty

This method will silently fail to set "writable", "enumerable", and "configurable" properties.

Providing a getter or setter with "get" or "set" on a descriptor will silently fail on engines that lack "defineGetter" and "defineSetter", which include all versions of IE up to version 8 so far.

IE 8 provides a version of this method but it only works on DOM objects. Thus, the shim will not get installed and attempts to set "value" properties will fail silently on non-DOM objects.

https://github.com/kriskowal/es5-shim/issues#issue/5

So you know your answer. It can be done on DOM elements, that's it (and on IE8 only).

I'd suggest you just use get/set methods if you want IE7 to work.

like image 117
Florian Margaine Avatar answered Sep 20 '22 10:09

Florian Margaine


For older IEs you'd have to make sure your property is a dom object (even a fake tag) and use onPropertyChange to get notified. See this post by John Dyer for more details.

like image 24
MandoMando Avatar answered Sep 22 '22 10:09

MandoMando