Don't assign one object to another one

correct way, when changing object, firstly you should create this object and then assign its property to the new object

<!DOCTYPE html>

<html lang="en">
<head>
    <title>Chapter 11: Example 7</title>
</head>
<body>
    <form action="" name="theForm">
        <select name="theDay" size="5">
            <option value="0" selected="selected">Monday</option>
            <option value="1">Tuesday</option>
            <option value="2">Wednesday</option>
            <option value="3">Thursday</option>
            <option value="4">Friday</option>
            <option value="5">Saturday</option>
            <option value="6">Sunday</option>
        </select>
        <br />
        <input type="button" value="Remove Wednesday" name="btnRemoveWed" />
        <input type="button" value="Add Wednesday" name="btnAddWed" />
        <br />
    </form>

    <script>
        var theForm = document.theForm;

        function btnRemoveWedClick() {
            var options = theForm.theDay.options;

            if (options[2].text == "Wednesday") {
                options[2] = null;
            } else {
                alert("There is no Wednesday here!");
            }
        }
        //change object value
        function btnAddWedClick() {
            var options = theForm.theDay.options;
            console.log('length ' +options.length); //6
            if (options[2].text != "Wednesday") {
                var lastOption = new Option();
                options[options.length] = lastOption;

                for (var index = options.length - 1; index > 2; index--) {
                    console.log(index);  // 6  because you add last option to  options
                    var currentOption = options[index];
                    var previousOption = options[index - 1];

                    currentOption.text = previousOption.text;
                    currentOption.value = previousOption.value;
                }

                var option = new Option("Wednesday", 2);
                options[2] = option;
            } else {
                alert("Do you want to have TWO Wednesdays?");
            }
        }

        theForm.btnRemoveWed.addEventListener("click", btnRemoveWedClick);
        theForm.btnAddWed.addEventListener("click", btnAddWedClick);
    </script>
</body>
</html>

wrong way. don't assign one object to another one , this won't work

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
<form action="" name="theForm">
    <select name="theDay" size="5">
        <option value="0" selected="selected">Monday</option>
        <option value="1">Tuesday</option>
        <option value="2">Wednesday</option>
        <option value="3">Thursday</option>
        <option value="4">Friday</option>
        <option value="5">Saturday</option>
        <option value="6">Sunday</option>
    </select>
    <br />
    <input type="button" value="Remove Wednesday" name="btnRemoveWed" />
    <input type="button" value="Add Wednesday" name="btnAddWed" />
    <br />
</form>
<script>
    var theDayEls = document.theForm.theDay;
    document.write('option length is :');
    document.write(theDayEls.length + ', ' + theDayEls.options.length +'<br>');

    console.log(document.theForm.theDay.options)
    for (let i=0; i<theDayEls.options.length; i++){
        document.write('   |  value :');
        document.write(theDayEls.options[i].value);
        document.write(', text :');
        document.write(theDayEls.options[i].text);
    }

    var btnRemoveWed = document.theForm.btnRemoveWed;
    var btnAddWed = document.theForm.btnAddWed;
    btnRemoveWed.addEventListener('click',btnRemoveWedClick);
    btnAddWed.addEventListener('click',btnAddWedClick);

    function btnRemoveWedClick(e) {
        var options = document.theForm.theDay.options;
        //iterate 7 times, not user-friendly
        // for (let option of options){
        //
        //     if (option.text == options[2].text){
        //         console.log('before');
        //         console.log(options[2]);
        //         console.log(option);
        //   //      option= null; //for of loop can not change the value of collections
        //         //copy the options elements to options for reading
        //        options[2]= null;  //really remove
        //         console.log('after');
        //         console.log(options[2]);  // turn to options[3] value
        //         console.log(option); //still exists
        //     }else {
        //         alert('no more to be removed');
        //     }
        // }
  if (options[2].text == 'Wednesday'){
            options[2] = null;
        }else {
            alert('no more to be removed');
        }
   }
   //can not assign the option object to another option object 
    //totally wrong
    function btnAddWedClick() {
        var options =document.theForm.theDay.options;

        // var lastOption = options[options.length]; //wrong way
        // console.log(lastOption); //undefined

        var lastOpt = new Option();
      //  options[options.length] = lastOpt;
        options[options.length] = options[6];
        lastOpt =options[6];
        console.log(options[5])
        console.log(lastOpt)
        var prevOpt;
        // for (var i = options.length-1 ; i >= 3 ; i--){
        //      prevOpt = options[i-1];
        //     var currOpt = options[i];
        //     var nextOpt = options[i+1];
        //     nextOpt = currOpt;
        //     currOpt = prevOpt;
        //  //   console.log(i-1 +'inside for' +(options[2] == prevOpt)); //true
        // }
        var newWenOpt = new Option('Wednesday',2);
       // console.log(prevOpt);
       // prevOpt = newWenOpt;
       options[2] = newWenOpt;
        console.log('outside for' +(options[2] == prevOpt)); //false, there are not the same one
    }
</script>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
<form action="" name="theForm">
    <select name="theDay" size="5">
        <option value="0" selected="selected">Monday</option>
        <option value="1">Tuesday</option>
        <option value="2">Wednesday</option>
        <option value="3">Thursday</option>
        <option value="4">Friday</option>
        <option value="5">Saturday</option>
        <option value="6">Sunday</option>
    </select>
    <br />
    <input type="button" value="Remove Wednesday" name="btnRemoveWed" />
    <input type="button" value="Add Wednesday" name="btnAddWed" />
    <br />
</form>
<script>
    var theDayEls = document.theForm.theDay;
    document.write('option length is :');
    document.write(theDayEls.length + ', ' + theDayEls.options.length +'<br>');

    console.log(document.theForm.theDay.options)
    for (let i=0; i<theDayEls.options.length; i++){
        document.write('   |  value :');
        document.write(theDayEls.options[i].value);
        document.write(', text :');
        document.write(theDayEls.options[i].text);
    }

    var btnRemoveWed = document.theForm.btnRemoveWed;
    var btnAddWed = document.theForm.btnAddWed;
    btnRemoveWed.addEventListener('click',btnRemoveWedClick);
    btnAddWed.addEventListener('click',btnAddWedClick);

    function btnRemoveWedClick(e) {
        var options = document.theForm.theDay.options;
        //iterate 7 times, not user-friendly
        // for (let option of options){
        //
        //     if (option.text == options[2].text){
        //         console.log('before');
        //         console.log(options[2]);
        //         console.log(option);
        //   //      option= null; //for of loop can not change the value of collections
        //         //copy the options elements to options for reading
        //        options[2]= null;  //really remove
        //         console.log('after');
        //         console.log(options[2]);  // turn to options[3] value
        //         console.log(option); //still exists
        //     }else {
        //         alert('no more to be removed');
        //     }
        // }
  if (options[2].text == 'Wednesday'){
            options[2] = null;
        }else {
            alert('no more to be removed');
        }
   }
   //can not assign the option object to another option object 
    //totally wrong
    function btnAddWedClick() {
        var options =document.theForm.theDay.options;

        // var lastOption = options[options.length]; //wrong way
        // console.log(lastOption); //undefined

        var lastOpt = new Option();
      //  options[options.length] = lastOpt;
        options[options.length] = options[6];
        lastOpt =options[6];
        console.log(options[5])
        console.log(lastOpt)
        var prevOpt;
        // for (var i = options.length-1 ; i >= 3 ; i--){
        //      prevOpt = options[i-1];
        //     var currOpt = options[i];
        //     var nextOpt = options[i+1];
        //     nextOpt = currOpt;
        //     currOpt = prevOpt;
        //  //   console.log(i-1 +'inside for' +(options[2] == prevOpt)); //true
        // }
        var newWenOpt = new Option('Wednesday',2);
       // console.log(prevOpt);
       // prevOpt = newWenOpt;
       options[2] = newWenOpt;
        console.log('outside for' +(options[2] == prevOpt)); //false, there are not the same one
    }
</script>
</body>
</html>
posted @ 2020-02-27 18:41  Coding_Changes_LIfe  阅读(145)  评论(0编辑  收藏  举报