Calling Array.splice({}) with no start

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dhtml

    Calling Array.splice({}) with no start

    Array.splice({} )

    What should it do?

    I think it should return a new Array with length 0.

    Array.splice(ar r, start, deleteCount [, item1 [, item2[,...]]])


    Example:
    Array.splice({} )

    Result:
    FF3.1
    function splice() { [native code] }
    Webkit:
    undefined


    If - start - is passed in, a 0 length array is returned.

    Array.prototype .splice.call(0, undefined);
    Result: []

    The methods that go into calculating the - length - property all call
    ToNumber, either through ToUint32, or ToInteger. ToNumber coverts
    undefined to NaN, which returns the value back to its caller, either
    ToInteger or ToUint32, which converts NaN to +0.


    It seems step 4 is not what I expect:
    | 4. Call ToInteger(start ).

    start is omitted from the argument list, I assume this means start =
    undefined.

    ToInteger calls ToNumber.
    ToInteger:
    | 1. Call ToNumber on the input argument.
    | 2. If Result(1) is NaN, return +0.
    ....

    ToNumber returns NaN:
    | 9.3 ToNumber
    | The operator ToNumber converts its argument
    | to a value of type Number according to the following table:
    |-----------+-----+
    | Undefined | NaN |
    `....-------+-----+

    Back to ToInteger, step 2.
    | 2. If Result(1) is NaN, return +0.

  • dhtml

    #2
    Re: Calling Array.splice({} ) with no start

    dhtml wrote:

    That would be:
    javascript:aler t(Array.prototy pe.isPrototypeO f(
    Array.prototype .splice.call({} ) ))

    Results:
    Opera9.5 mac:
    true
    FF3.1, Webkit
    false

    Garrett

    Comment

    • Robin Rattay

      #3
      Re: Calling Array.splice({} ) with no start

      On 19 Aug., 07:59, dhtml <dhtmlkitc...@g mail.comwrote:
      Array.splice({} )
      >
      What should it do?
      >
      I think it should return a new Array with length 0.
      >
      Array.splice(ar r, start, deleteCount [, item1 [, item2[,...]]])http://bclary.com/2004/11/07/#a-15.4.4.12
      I'm only an occasional poster and not expert, but am I falling for a
      troll here?

      Array.splice({} ) makes semantically absolutely no sense.

      a) Splice is the method of an array object, not of the Array global
      object.
      b) "{}" is an empty object not an "array of the length 0".

      So I guess you accually mean [].splice()

      However
      c) the start and deleteCount parameters are mandatory, so who cares
      what the different engines return in such an undefined case?

      Robin

      Comment

      • Henry

        #4
        Re: Calling Array.splice({} ) with no start

        On Aug 19, 2:40 pm, Robin Rattay wrote:
        On 19 Aug., 07:59, dhtml wrote:
        >Array.splice({ })
        >
        >What should it do?
        >
        >I think it should return a new Array with length 0.
        >
        >Array.splice(a rr, start, deleteCount [, item1 [, item2[,...]]])
        >http://bclary.com/2004/11/07/#a-15.4.4.12
        >
        I'm only an occasional poster and not expert, but am
        I falling for a troll here?
        It would have been easier to tell if the OP had actually made some
        (any) point.
        Array.splice({} ) makes semantically absolutely no sense.
        >
        a) Splice is the method of an array object, not of the
        Array global object.
        That may just be a misguided use of "Array" to mean any array object.
        There are certainly many less ambiguous ways to express that, if it
        was the intention.
        b) "{}" is an empty object not an "array of the length 0".
        >
        So I guess you accually mean [].splice()
        More likely - [].slice({}) - else there would have been no point in
        mentioning the handling of objects in ToInteger (which without a
        modification to Object.prototyp e.toString/valueOf will return numeric
        zero).
        However
        c) the start and deleteCount parameters are mandatory,
        No they are not (at least in the sense that omitting them form the
        method call will not result in an error and will have a predictable/
        specified outcome).
        so who cares what the different engines return in such
        an undefined case?
        If the outcome does not correspond with the specified outcome in any
        given implementation then those implementers might care.

        Comment

        • dhtml

          #5
          Re: Calling Array.splice({} ) with no start

          Henry wrote:
          On Aug 19, 2:40 pm, Robin Rattay wrote:
          >On 19 Aug., 07:59, dhtml wrote:
          >>Array.splice( {})
          >>What should it do?
          >>I think it should return a new Array with length 0.
          >>Array.splice( arr, start, deleteCount [, item1 [, item2[,...]]])
          >>http://bclary.com/2004/11/07/#a-15.4.4.12
          >I'm only an occasional poster and not expert, but am
          >I falling for a troll here?
          >
          No, you're trying to answer a question you don't have an answer to and
          being mildly insulting in the process. What makes you think I'm a troll?
          It would have been easier to tell if the OP had actually made some
          (any) point.
          Ah, but I asked a question.
          >
          >Array.splice({ }) makes semantically absolutely no sense.
          >>
          >a) Splice is the method of an array object, not of the
          >Array global object.
          >
          Right, Array.splice is the generic top-level method (and 'Henry' is
          likely aware). But that's why I followed up with the
          Array.prototype .splice.call sample, which is standard and implemented in
          more browsers.
          That may just be a misguided use of "Array" to mean any array object.
          Nope, just the code I was running in Firefox.
          >
          >b) "{}" is an empty object not an "array of the length 0".
          >>
          >So I guess you accually mean [].splice()
          >
          More likely - [].slice({}) -
          No, I meat exactly what I wrote, and if you'd tried it, you might have
          noticed that Array.splice({} ) in Firefox, returns the splice function
          itself like I wrote. Did you try it?
          >However
          >c) the start and deleteCount parameters are mandatory,
          >
          No they are not (at least in the sense that omitting them form the
          method call will not result in an error and will have a predictable/
          specified outcome).
          >
          >so who cares what the different engines return in such
          >an undefined case?
          >
          If the outcome does not correspond with the specified outcome in any
          given implementation then those implementers might care.
          The question is: "What is the specified output when 'start' is absent?"

          Its a somewhat obscure question. I think it should be the array created
          in step 1. Webkit and Firefox give different results.

          Related thread:


          Regarding step 4 of Array.prototype .splice, if the 'start' argument is
          not present, it should be assumed to be undefined, right? This lead
          through several more steps, which should result in step 54 returning A.

          Garrett

          Comment

          • Henry

            #6
            Re: Calling Array.splice({} ) with no start

            On Aug 19, 6:50 pm, dhtml wrote:
            Henry wrote:
            >On Aug 19, 2:40 pm, Robin Rattay wrote:
            >>On 19 Aug., 07:59, dhtml wrote:
            >>>Array.splice ({})
            >>>What should it do?
            >It would have been easier to tell if the OP had actually
            >made some
            >(any) point.
            >
            Ah, but I asked a question.
            If you mean your question was "what should a non-standard language
            extension that only exists in a few of implementations do?" then the
            answer is "anything it likes, and not necessarily the same thing as
            any other non-standard extension that resembles it".
            >>Array.splice( {}) makes semantically absolutely no sense.
            >
            >>a) Splice is the method of an array object, not of the
            >>Array global object.
            >
            Right, Array.splice is the generic top-level method
            There is no "the" about it. If there is an - Array.splice - at all
            then it is a non-standard language extension.
            (and 'Henry' is
            likely aware). But that's why I followed up with the
            Array.prototype .splice.call sample, which is standard
            and implemented in more browsers.
            Is it? I did wonder what the point of that follow up was, particularly
            as you switched from using an array literal as the argument to splice
            to not providing any arguments at all.
            >That may just be a misguided use of "Array" to mean any
            >array object.
            >
            Nope, just the code I was running in Firefox.
            So it was actually a pointless question; it is generally not viable to
            use non-standard language extensions in non-known environment contexts
            (as they are unlikely to be either universally or consistently
            implemented) and if you have a know environment you can determine the
            answer for that context on your own.
            >>b) "{}" is an empty object not an "array of the length 0".
            >
            >>So I guess you accually mean [].splice()
            >
            >More likely - [].slice({}) -
            >
            No, I meat exactly what I wrote, and if you'd tried it, you
            might have noticed that Array.splice({} ) in Firefox, returns
            the splice function itself like I wrote.
            You did not write that.
            Did you try it?
            No, why should I as you made no relevant point?

            <snip>
            The question is: "What is the specified output when 'start'
            is absent?"
            <snip>

            With - Array.prototype .slice -, an empty array.
            With - Array.slice -, ask the people who wrote it.

            Comment

            • dhtml

              #7
              Re: Calling Array.splice({} ) with no start

              Henry wrote:
              On Aug 19, 6:50 pm, dhtml wrote:
              >Henry wrote:
              >>On Aug 19, 2:40 pm, Robin Rattay wrote:
              >>>On 19 Aug., 07:59, dhtml wrote:
              [snip]
              >>>b) "{}" is an empty object not an "array of the length 0".
              >>>So I guess you accually mean [].splice()
              >>More likely - [].slice({}) -
              >No, I meat exactly what I wrote, and if you'd tried it, you
              >might have noticed that Array.splice({} ) in Firefox, returns
              >the splice function itself like I wrote.
              >
              You did not write that.
              >
              Anyone can read that first post:
              | Example:
              | Array.splice({} )
              |
              |Result:
              | FF3.1
              | function splice() { [native code] }
              |

              Firefox 3.1 returning the splice function itself.

              The funny thing is, is that if -undefined- is passed in, it works as
              expected in Firefox.

              javascript:var ap = Array.prototype ;
              alert(ap.isProt otypeOf(ap.spli ce.call({})));
              FF3.1, Webkit: false
              Opera: true

              javascript:aler t(ap.isPrototyp eOf(ap.splice.c all({}, undefined)));
              FF3.1, Webkit: true
              Opera: true

              I think Opera is getting this right. I've read through the steps of the
              algorithm an the Array created in step 1 should be returned in step 54.
              If I'm not mistaken, that's the only Return.

              Garrett

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Calling Array.splice({} ) with no start

                dhtml wrote:
                Henry wrote:
                >On Aug 19, 6:50 pm, dhtml wrote:
                >>Henry wrote:
                >>>On Aug 19, 2:40 pm, Robin Rattay wrote:
                >>>>On 19 Aug., 07:59, dhtml wrote:
                >
                [snip]
                >>>>b) "{}" is an empty object not an "array of the length 0".
                >>>>So I guess you accually mean [].splice()
                >>>More likely - [].slice({}) -
                >>No, I meat exactly what I wrote, and if you'd tried it, you
                >>might have noticed that Array.splice({} ) in Firefox, returns
                >>the splice function itself like I wrote.
                >You did not write that.
                >
                Anyone can read that first post:
                | Example:
                | Array.splice({} )
                |
                |Result:
                | FF3.1
                | function splice() { [native code] }
                |
                >
                Firefox 3.1 returning the splice function itself.
                Firefox 3.1 is already released? Time flies ...
                The funny thing is, is that if -undefined- is passed in, it works as
                expected in Firefox.
                >
                javascript:var ap = Array.prototype ;
                alert(ap.isProt otypeOf(ap.spli ce.call({})));
                FF3.1, Webkit: false
                Opera: true
                >
                javascript:aler t(ap.isPrototyp eOf(ap.splice.c all({}, undefined)));
                FF3.1, Webkit: true
                Opera: true
                >
                I think Opera is getting this right. I've read through the steps of the
                algorithm an the Array created in step 1 should be returned in step 54.
                If I'm not mistaken, that's the only Return.
                You don't get it, do you? The language standard makes the first two
                arguments of the method *mandatory*, so if an implementation does not error
                out if one or both are missing, there is no telling if the return value is
                the right or wrong one because there is nothing to base that assessment on.

                And Array.splice() is certainly not the same as Array.prototype .splice().


                PointedEars
                --
                Use any version of Microsoft Frontpage to create your site.
                (This won't prevent people from viewing your source, but no one
                will want to steal it.)
                -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

                Comment

                • dhtml

                  #9
                  Re: Calling Array.splice({} ) with no start

                  Thomas 'PointedEars' Lahn wrote:
                  dhtml wrote:
                  >Henry wrote:
                  >>On Aug 19, 6:50 pm, dhtml wrote:
                  >>>Henry wrote:
                  >>>>On Aug 19, 2:40 pm, Robin Rattay wrote:
                  >>>>>On 19 Aug., 07:59, dhtml wrote:
                  You don't get it, do you? The language standard makes the first two
                  arguments of the method *mandatory*,
                  Apparently I missed the line in the spec that says 'The first two
                  arguments are necessary.'

                  About the closest I can get to supporting your claim is:

                  | When the splice method is called with
                  | two or more arguments start...
                  | The following steps are taken:

                  Which could mean that: The algorithm applies under the conditions of
                  both -start- and -deleteCount- being present. Or, the sentences could be
                  taken separately.

                  The wording isn't clear.

                  so if an implementation does not error
                  out if one or both are missing, there is no telling if the return value is
                  the right or wrong one because there is nothing to base that assessment on.
                  >
                  There's the algorithm for splice. Did you read it?
                  And Array.splice() is certainly not the same as Array.prototype .splice().
                  >
                  It's actually the same code in the engine, but that's really beside the
                  point.

                  Please try to keep this thread focused and not destroy it.

                  Garrett
                  >
                  PointedEars

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Calling Array.splice({} ) with no start

                    dhtml wrote:
                    Thomas 'PointedEars' Lahn wrote:
                    >You don't get it, do you? The language standard makes the first two
                    >arguments of the method *mandatory*,
                    >
                    Apparently I missed the line in the spec that says 'The first two
                    arguments are necessary.' [...]
                    *Obviously*.

                    | 15.4.4.12 Array.prototype .splice (start, deleteCount [ , item1 [ , item2 [
                    | , … ] ] ] )

                    Brackets usually mark optional arguments/parameters, and they do so there.


                    PointedEars
                    --
                    Use any version of Microsoft Frontpage to create your site.
                    (This won't prevent people from viewing your source, but no one
                    will want to steal it.)
                    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

                    Comment

                    • dhtml

                      #11
                      Re: Calling Array.splice({} ) with no start

                      Thomas 'PointedEars' Lahn wrote:
                      dhtml wrote:
                      >Thomas 'PointedEars' Lahn wrote:
                      >>You don't get it, do you? The language standard makes the first two
                      >>arguments of the method *mandatory*,
                      >Apparently I missed the line in the spec that says 'The first two
                      >arguments are necessary.' [...]
                      >
                      *Obviously*.
                      >
                      | 15.4.4.12 Array.prototype .splice (start, deleteCount [ , item1 [ , item2 [
                      | , … ] ] ] )
                      >
                      Brackets usually mark optional arguments/parameters, and they do so there.
                      >
                      Usually. But the spec has examples of sections that don't seem to follow
                      that convention. For example:

                      What about slice:
                      15.4.4.10 Array.prototype .slice(start, end)

                      or sort:
                      15.4.4.11 Array.prototype .sort(comparefn )

                      These don't have brackets around optional arguments. The arguments are
                      optional in all modern (non-beta) implementations .

                      slice is very commonly used to convert an object to an array, and is
                      often used with no arguments. For example:

                      Array.prototype .slice.call(arg uments);

                      There is a general rule established by most (if not all) other methods
                      that if an argument is missing, it is treated as undefined.

                      The "optional arguments are bracketed" rule is not mentioned in the
                      Notational Conventions section, nor is there any text in the document
                      that says what should happen if an argument is missing.

                      Garrett
                      >
                      PointedEars

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: Calling Array.splice({} ) with no start

                        dhtml wrote:
                        Thomas 'PointedEars' Lahn wrote:
                        >dhtml wrote:
                        >>Thomas 'PointedEars' Lahn wrote:
                        >>>You don't get it, do you? The language standard makes the first two
                        >>>arguments of the method *mandatory*,
                        >>Apparently I missed the line in the spec that says 'The first two
                        >>arguments are necessary.' [...]
                        >*Obviously*.
                        >>
                        >| 15.4.4.12 Array.prototype .splice (start, deleteCount [ , item1 [ , item2 [
                        >| , … ] ] ] )
                        >>
                        >Brackets usually mark optional arguments/parameters, and they do so there.
                        >
                        Usually. But the spec has examples of sections that don't seem to follow
                        that convention. For example:
                        >
                        What about slice:
                        15.4.4.10 Array.prototype .slice(start, end)
                        >
                        or sort:
                        15.4.4.11 Array.prototype .sort(comparefn )
                        >
                        These don't have brackets around optional arguments. The arguments are
                        optional in all modern (non-beta) implementations .
                        Non sequitur.

                        In the first case, either you are observing unspecified behavior or the
                        specification prose has to be considered too imprecise there as the provided
                        evidence shows.

                        In the second case, there is "If comparefn is not undefined" and "Otherwise
                        the following steps are taken." which would indicate that this argument of
                        this method is optional since according to section 10.1.3 extra formal
                        parameters have the value `undefined'.

                        Please do not quote signatures unless you refer to them.


                        PointedEars
                        --
                        realism: HTML 4.01 Strict
                        evangelism: XHTML 1.0 Strict
                        madness: XHTML 1.1 as application/xhtml+xml
                        -- Bjoern Hoehrmann

                        Comment

                        • dhtml

                          #13
                          Re: Calling Array.splice({} ) with no start

                          Thomas 'PointedEars' Lahn wrote:
                          dhtml wrote:
                          >Thomas 'PointedEars' Lahn wrote:
                          >>dhtml wrote:
                          >>>Thomas 'PointedEars' Lahn wrote:
                          >>>>You don't get it, do you? The language standard makes the first two
                          >>>>arguments of the method *mandatory*,
                          >>>Apparently I missed the line in the spec that says 'The first two
                          >>>arguments are necessary.' [...]
                          >>*Obviously* .
                          >>>
                          >>| 15.4.4.12 Array.prototype .splice (start, deleteCount [ , item1 [ , item2 [
                          >>| , … ] ] ] )
                          >>>
                          >>Brackets usually mark optional arguments/parameters, and they do so there.
                          >Usually. But the spec has examples of sections that don't seem to follow
                          >that convention. For example:
                          >>
                          >What about slice:
                          >15.4.4.10 Array.prototype .slice(start, end)
                          >>
                          >or sort:
                          >15.4.4.11 Array.prototype .sort(comparefn )
                          >>
                          >These don't have brackets around optional arguments. The arguments are
                          >optional in all modern (non-beta) implementations .
                          >
                          Non sequitur.
                          >
                          Shows that optional arguments are treated as undefined.
                          In the first case, either you are observing unspecified behavior or the
                          specification prose has to be considered too imprecise there as the provided
                          evidence shows.
                          It is the case that slice treats the unspecified params as undefined.
                          >
                          In the second case, there is "If comparefn is not undefined" and "Otherwise
                          the following steps are taken." which would indicate that this argument of
                          this method is optional since according to section 10.1.3 extra formal
                          parameters have the value `undefined'.
                          >
                          Right. As stated in 10.1.3, if the caller supplies fewer parameters than
                          formal parameters, the extra parameter variables have the value
                          undefined. And splice() is a function.

                          IOW, When an argument is absent, undefined is used as the value. The
                          only exception I find is FF/Webkit's implentation splice. Both treat
                          "argument missing" as something different from undefined.

                          [].splice.call({} )
                          FF3, Webkit both return undefined
                          Opera 9.5 returns 0 length array.
                          [].splice.call({} ,undefined)
                          FF3, Webkit, Opera 9.5 all return a 0 length array.

                          Garrett

                          Please do not quote signatures unless you refer to them.
                          >
                          >
                          PointedEars

                          Comment

                          • dhtml

                            #14
                            Re: Calling Array.splice({} ) with no start

                            dhtml wrote:
                            Thomas 'PointedEars' Lahn wrote:
                            >dhtml wrote:
                            >>Thomas 'PointedEars' Lahn wrote:
                            >>>dhtml wrote:
                            >>>>Thomas 'PointedEars' Lahn wrote:
                            [].splice.call({} )
                            FF3, Webkit both return undefined
                            FF3 returns the splice function.
                            Opera 9.5 returns 0 length array.
                            [].splice.call({} ,undefined)
                            FF3, Webkit, Opera 9.5 all return a 0 length array.
                            >
                            Garrett
                            >>
                            >PointedEars

                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: Calling Array.splice({} ) with no start

                              dhtml wrote:
                              Thomas 'PointedEars' Lahn wrote:
                              >dhtml wrote:
                              >>Thomas 'PointedEars' Lahn wrote:
                              >>>dhtml wrote:
                              >>>>Thomas 'PointedEars' Lahn wrote:
                              >>>>>You don't get it, do you? The language standard makes the first two
                              >>>>>argument s of the method *mandatory*,
                              >>>>Apparentl y I missed the line in the spec that says 'The first two
                              >>>>arguments are necessary.' [...]
                              >>>*Obviously *.
                              >>>>
                              >>>| 15.4.4.12 Array.prototype .splice (start, deleteCount [ , item1 [ , item2 [
                              >>>| , … ] ] ] )
                              >>>>
                              >>>Brackets usually mark optional arguments/parameters, and they do so there.
                              >>Usually. But the spec has examples of sections that don't seem to follow
                              >>that convention. For example:
                              >>>
                              >>What about slice:
                              >>15.4.4.10 Array.prototype .slice(start, end)
                              >>>
                              >>or sort:
                              >>15.4.4.11 Array.prototype .sort(comparefn )
                              >>>
                              >>These don't have brackets around optional arguments. The arguments are
                              >>optional in all modern (non-beta) implementations .
                              >Non sequitur.
                              >
                              Shows that optional arguments are treated as undefined.
                              If anything, it shows that that the specification is worded imprecisely.
                              Which point are you trying to make here? It was never debated that formal
                              parameters for which no value has been passed are treated as undefined *on
                              call*, that is what is specified. (Why did you reiterate that anyway?)

                              But what that means for the underlying algorithm of the method and therefore
                              for its return value, is an entirely different matter. If the Specification
                              does not say how an implementation is supposed to behave in such a case, it
                              is wrong to say that one implementation is correct and another one is not
                              just because the outcomes differ.
                              >Please do not quote signatures unless you refer to them.
                              >>
                              >PointedEars
                              ^^^^^^^^^^^
                              Which part of that did you not get?


                              PointedEars
                              --
                              Use any version of Microsoft Frontpage to create your site.
                              (This won't prevent people from viewing your source, but no one
                              will want to steal it.)
                              -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

                              Comment

                              Working...