The problem of passing parameters of js Asynchronous function

Code

var choosed = null; // 1
EventUtil.addHandler(lists, "click", cancel.bind(null, choosed)); // 2
EventUtil.addHandler(delSure, "click", delNote.bind(null, choosed)); // 3

problem description

  • the first line of code is used to save the button choosed being clicked as a global variable
  • every time the second line of code is clicked first, then the value of choosed is changed in the cancel function, pointing to the dom element being clicked,
  • when the third line of code is clicked, you need to get the value of choosed and find that choosed = null,

expected results

when the third line of code executes, choosed should be equal to the value changed by the second line of code, not null

debugging

suspects that choosed = null is not the original value, so it is changed to choosed = {} , and the result is still wrong, so refer to the netizen"s answer and change the code to: choosed = {} / / 1 when the second line changes to: choosed.a =", as a result choosed.a can be obtained on the third line, obviously this is a reference value, so why choosed = null; or choosed = {} < / code

question

how do you tell the difference between the original value and the reference value? , choosed = null; or choosed = {} , which I take as the original

Feb.15,2022

although typeof null = 'object', null cannot be used as a reference value. This is a problem left over from history, such as null.a = 1; it will report an error;

the previous answer deleted the wrong answer "(please answer") "

Menu