Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$.map have no scope bind #1328

Open
futurist opened this issue Jan 16, 2018 · 1 comment
Open

$.map have no scope bind #1328

futurist opened this issue Jan 16, 2018 · 1 comment

Comments

@futurist
Copy link

var arr=[{a:1},{a:2}]
$.map(arr, function(){console.log(this)})

Always print window, window

While as

var arr=[{a:1},{a:2}]
$.each(arr, function(){console.log(this)})

Print right output: {a:1}, {a:2}

@ghost
Copy link

ghost commented Jan 22, 2018

@futurist

I think $.map() is designed to work in this way intentionally.

The source code is shown as follows:

// $.each()
value = callback.call(elements[i], i, elements[i]);
// $.map()
value = callback(elements[i], i);

They are designed in a similar fashion as JQuery and the source code of JQuery is shown as follows:

// $.each()
value = callback.call( obj[ i ], i, obj[ i ] )
// $.map()
value = callback( elems[ i ], i, arg )

JQuery has mentioned that $.map() refers to the global (window) object and $.each() refers to an Object even if it is a simple string or number value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant