> Javascript

Trap of Javascript

1. Reference before assignment function ShuffleArray(array){ // inplace for (let i=array.length-1; i>0;i--){ var j = Math.floor(Math.random()*(i+1)); [arr[i], arr[j]] = [arr[j], arr[i]]; // no error } } This function not only cannot modify the array, but also affects the global variables! 2. Pass by reference (2d array) arr1 = [[0,0], [1,1]]; arr2 = arr1; console.log(arr1); // output: [[3,0], [1,1]] ??? // console.log(...arr1) instead arr2[0][0] = 3; console.log(arr2); // output: [[3,0], [1,1]] The output will be changed even before the assignment.

Continue reading

javascript

1. jQuery <!-- https://jquery.com/download/ --> <script src="{% static 'js/jquery-3.6.1.min.js' %}"></script> <script src="{% static 'plugins/bootstrap-3.4.1-dist/js/bootstrap.min.js' %}"></script> Bootstrap with jQuery will help us to create a lot of dynamic pages. <!-- Button trigger modal --> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> delete </button> <!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="myModalLabel">Warning</h4> </div> <div class="modal-body"> Are you sure that you want to delete this article?

Continue reading

1. Variable var a = 5; // declare global function myfunc(){ var a = 10; // override global { let a = 5; // local // here a = 5; } // here a = 10; } // only 'var' can override in the same block // var a = 10; // let a = 5; illegal statement 2. Constant const a = 5; // initial value must be assigned a = 3 // illegal statement // the atribution of constant object can be changed const b = ["Python", "Java", "C++"]; b[0] = "Ruby"; // legal statement // but the object itself can not be re-assigned b = ["C#", "VB"]; // illegal statment // more example const car = {type:"porsche", model:"911", color:"Black"}; car.

Continue reading

Author's picture

LI WEI

苟日新,日日新,又日新

Not yet

Tokyo