Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30587

Re: not "autovivification", but ...?

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: not "autovivification", but ...?
Date 2016-05-31 14:38 +0200
Organization albasani.net
Message-ID <nik0k5$8mu$1@news.albasani.net> (permalink)
References <autovivification-20160531121607@ram.dialup.fu-berlin.de>

Show all headers | View raw


Stefan Ram wrote:
>   If Perl's autovivification would exist in JavaScript, this
>   would mean that in a new interpreter instance, the evaluation
>   of the expression
> 
> a.b.c = 1;
> 
>   would implicitly create the object »a« and »a.b«
>   and then assign »1« to the property »c« of »a.b«.

Perl's autovivification is a special case. It looks very user-friendly at
first glance, because it can automatically create intermediate structures
when they're needed - but it also does that on access, not just on
assignment. Examining a value can modify it, which is a common source of bugs:

  my %hash;
  if (exists $hash{foo}{bar}{baz}) {
      say "this statement never executes";
  }

Looks innocent enough, but %hash has just been changed from `undef` to

  ( foo => { bar => {} } )

>   We do not have this in JavaScript, but we do have
> 
> "use strict"; this.b = 1;
> 
>   . That is, when »this.b« is a assigned to - even in strict mode -
>   a new property »b« is implicitly created. No declaration (with
>   »var«, »let«, or »const«) is required.
> 
>   Is there a name for this »feature« of JavaScript? 
>   Maybe »semivivification«, because it's like
>   autovivification, but not so strong?

I would advise against using the name (auto/semi)vivification, if only to
avoid the negative connotations this feature has in Perl (at least for
experienced programmers).

There is a perfectly good description for this behavior in JavaScript, and
you even used it yourself: implicit property creation.

In both languages, there are ways to prevent this implicit creation: the "no
autovivification" pragma in Perl; strict mode and object sealing or freezing
in JS.


- stefan

Back to comp.lang.javascript | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: not "autovivification", but ...? Stefan Weiss <krewecherl@gmail.com> - 2016-05-31 14:38 +0200
  Re: not "autovivification", but ...? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-01 08:03 -0700
    Re: not "autovivification", but ...? Stefan Weiss <krewecherl@gmail.com> - 2016-06-01 19:49 +0200
      Re: not "autovivification", but ...? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-01 14:29 -0700
        Re: not "autovivification", but ...? Stefan Weiss <krewecherl@gmail.com> - 2016-06-02 01:29 +0200
          Re: not "autovivification", but ...? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-02 02:16 +0200
            Re: not "autovivification", but ...? Stefan Weiss <krewecherl@gmail.com> - 2016-06-02 13:20 +0200
          Re: not "autovivification", but ...? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-02 15:51 -0700

csiph-web