Array.prototype[0] setter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Array.prototype[0] setter

Felix-54
If I define a setter for Array.prototype[0], does [].push invoke that setter?

Test code:

<!doctype html><html><body>
<script>
  Object.defineProperty(
    Array.prototype, 0,
    { get : function() { alert('get 0'); return this.zero; },
      set : function(v) { alert('set 0 ' + v); this.zero = v; },
      enumerable : true,
      configurable : false });
  var a = [];
  alert('before push a[0] = ' + a[0]);
  a.push(44);
  alert('after push a[0] = ' + a[0]);
</script>
</body></html>

On all the browsers I've tried so far, the setter doesn't get invoked.

However, it looks to me like the ES5.1 spec says the setter should get invoked:

15.4.4.7 says push() invokes the [[Put]] internal method

8.12.5 says [[Put]] tries to use an inherited property descriptor if
there isn't an own property descriptor.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.prototype[0] setter

Allen Wirfs-Brock

On Oct 18, 2011, at 12:31 PM, felix wrote:

> If I define a setter for Array.prototype[0], does [].push invoke that setter?

It's supposed to.

>
> Test code:
>
> <!doctype html><html><body>
> <script>
>  Object.defineProperty(
>    Array.prototype, 0,
>    { get : function() { alert('get 0'); return this.zero; },
>      set : function(v) { alert('set 0 ' + v); this.zero = v; },
>      enumerable : true,
>      configurable : false });
>  var a = [];
>  alert('before push a[0] = ' + a[0]);
>  a.push(44);
>  alert('after push a[0] = ' + a[0]);
> </script>
> </body></html>
>
> On all the browsers I've tried so far, the setter doesn't get invoked.
>
> However, it looks to me like the ES5.1 spec says the setter should get invoked:

Yes, that is what it says and it is intentional.

>
> 15.4.4.7 says push() invokes the [[Put]] internal method
>
> 8.12.5 says [[Put]] tries to use an inherited property descriptor if
> there isn't an own property descriptor.
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.prototype[0] setter

David Bruant-5
Are there tests for this?

David

Le 18/10/2011 21:37, Allen Wirfs-Brock a écrit :

> On Oct 18, 2011, at 12:31 PM, felix wrote:
>
>> If I define a setter for Array.prototype[0], does [].push invoke that setter?
> It's supposed to.
>
>> Test code:
>>
>> <!doctype html><html><body>
>> <script>
>>  Object.defineProperty(
>>    Array.prototype, 0,
>>    { get : function() { alert('get 0'); return this.zero; },
>>      set : function(v) { alert('set 0 ' + v); this.zero = v; },
>>      enumerable : true,
>>      configurable : false });
>>  var a = [];
>>  alert('before push a[0] = ' + a[0]);
>>  a.push(44);
>>  alert('after push a[0] = ' + a[0]);
>> </script>
>> </body></html>
>>
>> On all the browsers I've tried so far, the setter doesn't get invoked.
>>
>> However, it looks to me like the ES5.1 spec says the setter should get invoked:
> Yes, that is what it says and it is intentional.
>
>> 15.4.4.7 says push() invokes the [[Put]] internal method
>>
>> 8.12.5 says [[Put]] tries to use an inherited property descriptor if
>> there isn't an own property descriptor.
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.prototype[0] setter

Andrea Giammarchi-2
In reply to this post by Felix-54
In webkit nightly not even the getter gets invoked.

On Tue, Oct 18, 2011 at 9:31 PM, felix <[hidden email]> wrote:
If I define a setter for Array.prototype[0], does [].push invoke that setter?

Test code:

<!doctype html><html><body>
<script>
 Object.defineProperty(
   Array.prototype, 0,
   { get : function() { alert('get 0'); return this.zero; },
     set : function(v) { alert('set 0 ' + v); this.zero = v; },
     enumerable : true,
     configurable : false });
 var a = [];
 alert('before push a[0] = ' + a[0]);
 a.push(44);
 alert('after push a[0] = ' + a[0]);
</script>
</body></html>

On all the browsers I've tried so far, the setter doesn't get invoked.

However, it looks to me like the ES5.1 spec says the setter should get invoked:

15.4.4.7 says push() invokes the [[Put]] internal method

8.12.5 says [[Put]] tries to use an inherited property descriptor if
there isn't an own property descriptor.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.prototype[0] setter

Andrea Giammarchi-2
however, push does invoke [[Put]] but only in non array objects.

var a = {};
Object.defineProperty(
   a, "0",
   { get : function() { alert('get 0'); return this.zero; },
     set : function(v) { alert('set 0 ' + v); this.zero = v; },
     enumerable : true,
     configurable : false });
a.length = 0;
a.push = [].push;
 alert('before push a[0] = ' + a[0]);
 a.push(44);
 alert('after push a[0] = ' + a[0]);


On Mon, Oct 24, 2011 at 4:12 PM, Andrea Giammarchi <[hidden email]> wrote:
In webkit nightly not even the getter gets invoked.

On Tue, Oct 18, 2011 at 9:31 PM, felix <[hidden email]> wrote:
If I define a setter for Array.prototype[0], does [].push invoke that setter?

Test code:

<!doctype html><html><body>
<script>
 Object.defineProperty(
   Array.prototype, 0,
   { get : function() { alert('get 0'); return this.zero; },
     set : function(v) { alert('set 0 ' + v); this.zero = v; },
     enumerable : true,
     configurable : false });
 var a = [];
 alert('before push a[0] = ' + a[0]);
 a.push(44);
 alert('after push a[0] = ' + a[0]);
</script>
</body></html>

On all the browsers I've tried so far, the setter doesn't get invoked.

However, it looks to me like the ES5.1 spec says the setter should get invoked:

15.4.4.7 says push() invokes the [[Put]] internal method

8.12.5 says [[Put]] tries to use an inherited property descriptor if
there isn't an own property descriptor.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss