The Parameter problem of splice () method of JavaScript Array

recently encountered a puzzling problem when using the splice method. Don"t say too much about the code:
< script >

    var arr=[1,2,3,7,8,9,2,6,9,9,8,5,2,3,1];
    console.log("",arr);

    arr.splice(undefined,1)
    console.log("undefined",arr);

    arr.splice(null,1)
    console.log("null",arr);

    arr.splice(NaN,1)
    console.log("NaN",arr);

    arr.splice(true,1)
    console.log("true",arr);

    arr.splice(false,1)
    console.log("false",arr);

    arr.splice(" ",1)
    console.log("",arr);

    arr.splice("",1)
    console.log("",arr);

    arr.splice(new Object(),1)
    console.log("",arr);

    arr.splice([],1)
    console.log("",arr);

    arr.splice(2.3,1)
    console.log("",arr);        

    arr.splice(-2.3,1)
    console.log("",arr);

    arr.splice(-2,1)
    console.log("",arr);        

    arr.splice(-2,1)
    console.log("",arr);
</script>

the printed result is as follows;

clipboard.png
I am a little dizzy at this time. The first parameter of the splice method is to specify the starting position of replacement or deletion, which is supposed to be an index value. For non-numeric parameters, if it calls a number object, undefined should be converted into NaN, and an error should be reported, but the printer still prints the result normally

.

all the above situations have one thing in common. It seems that only after they have been converted into the number 0 can the result of the printer be displayed.
do you have a boss to explain? I don"t think there is a description of this situation on MDN

.
May.22,2021

all the above situations have one thing in common. It seems that only when they are converted to the number 0 can the result of the printer be shown
.

obviously not all start is treated as zero.

1. Let's start with what's written on MDN, that is, the case of negative integers.

for start , the MDN description is:

. If this parameter is negative, it means that, slice (- 2 is extracted from the penultimate element in the original array.) it means that the penultimate element in the original array is extracted to the last element (including the last element).

that is, for the example of arr.splice (- 2,1) in the title, arr = [9,8,2,1] before extraction, start is-2 + 4 = 3, so the behavior is equivalent to arr.splice (3,1), and the result 2 is extracted, which matches the description.

2. The rest is not clearly described by MDN. Here, take a look at ECMA-262 specification , which is illustrated here with the latest specification.

first find the description of 22.1.3.25 Array.prototype.splice :

the processing of start is done in these two steps, and the final value that works is actualStart.

clipboard.png

startToInteger:

clipboard.png

ToNumber....

clipboard.png

undefinedToNumberNaN;

ToInteger+0;

Splicemin(+0, length)+0;

undefinedstart+0

...


:Array.prototype.splice

:
ABCDEFG

ALet relativeStart be ? ToInteger(start).
BIf relativeStart < 0, let actualStart be max((len + relativeStart), 0); else let actualStart be min(relativeStart, len).

ToInteger

CLet number be ? ToNumber(argument).
DIf number is NaN, return +0.
EIf number is +0, -0, +, or -, return number.
FReturn the number value that is the same sign as number and whose magnitude is floor(abs(number)).

ToNumber

FLAG
G Undefined NaN
H Null +0
I Boolean true---1 false---0
J String 0NaN
K Object ToPrimitive(argument, hint Number) toNumber


undefined: C(G)D 0
null: C(H)E 0
NaN: D 0
true: C(I) 1
false: C(I) 0
" ": C(J)D 0
'':: C(J)D 0
new Object(): C(K)D 0
[]: C(K)D 0
2.3: F 2
-2.3: FB 5-2=3
-2: B 4-2=2


NaN0

clipboard.png

clipboard.png

the decimal part is directly discarded from the decimal part

the main reason why you are confused is that there is some coordination between the sample data and the sample data, so it is difficult to distinguish

.

the first parameter will do

when it comes in.
  1. if it is Boolean, true is regarded as 1 li false as 0 < / Boolean >
  2. the rest of the cases are handled with parseInt, and if you get a NaN, it will be treated as zero. Otherwise, the converted value will be used.

echo 1212
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36c71-e58e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36c71-e58e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?