How does jQuery protect overwriting jQuery and $

后端 未结 5 1394
猫巷女王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:22

    The purpose of those lines is to be able to restore the original $ and jQuery global variables in case jQuery itself overrides them. It doesn't do anything to protect other code from overriding jQuery.

    If you want to protect your own namespace, you can do a setInterval that checks if the global variable is still an instanceof your object (only if your object is protected inside a closure, otherwise it can be modified too). But, this isn't good practice, as the idea of javascript is to be able to extend and customize. Put the control in the hands of the developer, don't try and "lock in" your objects.

提交回复
热议问题