The use and implementation principles of Noconflict () in JavaScript frameworks such as Jquery/underscore

Source: Internet
Author: User

The generic JavaScript framework is implemented by injecting its own properties into the Global object window. For example, jquery adds a "$" object to the window, and underscore adds an "_" object to the Window object. What if these global variables already exist before the framework is loaded?

<script>var $ = "$"; </script><script src= "Jquery-1.10.2.js" ></script><script>alert ($ = = = "$");//falsealert ($ (). jquery);//1.10.2</script>

Obviously $ is occupied by the jquery framework, and the value of the $ variable that we defined previously is overwritten. If you want to use the jquery framework, you want to use the $ variables you defined earlier. Then the Noconflict () function comes in handy.

<script>var $ = "$"; </script><script src= "Jquery-1.10.2.js" ></script><script>var Stilljquery = $.noconflict (); alert (Stilljquery (). jquery);//1.10.2alert ($ = = = "$");//true</script>

The general JavaScript Framework is the following structure, the implementation principle is as follows:

(function (window) {///save data var _$e = window. $E; var myplugin = {"Name": "aty"};myplugin.noconflict = function () {window. $E = _$e;return myplugin;};/ /register $ewindow with the Global object. $E = Myplugin;}) (window);


The use and implementation principles of Noconflict () in JavaScript frameworks such as Jquery/underscore

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.