Chrome DevTools Command Line API, jQuery, Dollar Sign Variable

Sorry for the title because I don't know how to pinpoint the exact problem from the time of writing ...

The problem I am seeing now is that under some certain conditions the dollar sign ( $

) variable from the DevTools Console is not overridden after jQuery is loaded.

Environment

  • Open the DevTools console to the page where NOT isjQuery

    loaded .
  • Run test cases in DevTools console.
  • Reload the page after each test case to jQuery

    be unloaded.
  • Chrome version: 37.0.2062.120 m

Preparation code :

function loadJqueryThenFire(func) {
    if (!window.jQuery) {  
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = '//code.jquery.com/jquery-1.11.1.js';
        s.onload = function() {
            func();
            this.parentNode.removeChild(this);
        };
        document.getElementsByTagName('head')[0].appendChild(s);
    } else {
        func();
    }
}

      

Test case 1 (anonymous function expression):

test = function() { console.log($); };
// this returns Console "$" object
loadJqueryThenFire(test);

      

Test Case 2 (function name):

test = function test() { console.log($); };
// this returns Console "$" object
loadJqueryThenFire(test);

      

Test Case 3 (anonymous function expression passed as parameter):

// this returns Console "$" object
loadJqueryThenFire(function() { console.log($); }); 

      

Test Case 4 (the named function expression is passed as a parameter):

// this returns Console "$" object
loadJqueryThenFire(function test() { console.log($); }); 

      

Test Case 5 (function declaration):

function test() { console.log($); };
// this returns jQuery "$" object (which is what I am expecting)
loadJqueryThenFire(test); 

      

This behavior also occurs in the latest stable release of jQuery ( 2.1.1

).

My question is, can anyone explain why it is $

not overridden? Will it have to do with how jQuery

to declare it $

?

Edit:

Found some more peculiar behaviors ...

Test Case 6 (direct function expression):

(function() {
    function test() { console.log($); };
    // this returns Console "$" object
    loadJqueryThenFire(test);
})()

      

Test Case 7 (nested function declaration):

function tc7() {
    function test() { console.log($); };
    // this returns jQuery "$" object (which is what I am expecting)
    loadJqueryThenFire(test); 
}
tc7();

      

+3


source to share


1 answer


I was doing tests with Chrome Version 61.0.3163.100 and in all cases DevTools Console $ is overridden by jQuery.



Screenshot

0


source







All Articles