How does jQuery protect overwriting jQuery and $

后端 未结 5 1402
猫巷女王i
猫巷女王i 2020-12-29 16:56

These variables are located immediately after defining a local copy of jQuery in the jQuery source.

// Map over jQuery in case of overwrite
_jQuery = window         


        
5条回答
  •  被撕碎了的回忆
    2020-12-29 17:18

    Javascript doesn't have the means of providing the kind of protection you are looking for.

    JQuery isn't "protecting" those variables. It's just copying the references $ and jquery into two other variables. The code you have read is equivalent to:

    var obj1 = {}; /* create an empty object and reference it as obj1 */
    var obj2 = obj1; /* made a second reference to that same object */
    

    This code doesn't "protect" obj1. It's perfectly valid that your code later on changes the value of obj1:

    obj1 = 'foo'; /* now obj1 references a string */
    

    obj1 doesn't "magically retain its value"; after that line, it's just a string. But the object is still available in obj2.

提交回复
热议问题