Jquery not working?

firefox (v52.0), jquery

it works:

// example 1
$('html').find('body')

      

it works:

// example 2
var h
h=$('html')
h.find('body')

      

This does not work:

// example 3
var f
f=$('html').find
f('body')

      

I get

Error: permission denied to access property "ownerDocument"

why?

but this works:

// example 4
var a
a = x => $('html').find(x)
a('body')

      

+3


source to share


2 answers


Example 3 doesn't work because it find

gets called in the global context when you assign it f

. If you use call

and pass in a valid object jQuery

as context, the code works. try it

var f = $('html').find;
console.log(f.call($('html'), 'body').length)
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      

Run codeHide result




Also, example 4 works because it a

can be translated to the following code if written without an arrow function.

var a = function(x) {
  return $('html').find(x);
};

      

This is just example 1, but using a wrapper function to accept a parameter

+2


source


No jQuery doesn't work

By looking at the jQuery core source code: https://github.com/jquery/jquery/blob/master/src/core.js#L51

You can see that the hold state is in this

.

B f=$('html').find

, this

of has f

changed and no longer this

holds the $('html')

state



-

In this case, you are just proxying the method call. this

find

does not change.

var a
a x => $('html').find(x)
a('body')

      

+1


source







All Articles