Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
9f8cd6c4
Commit
9f8cd6c4
authored
Dec 15, 2010
by
Gianni Chiappetta
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixing $.proxy to work like (and use) Function.prototype.bind (ticket #7783)
http://bugs.jquery.com/ticket/7783
parent
c9c9057c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
39 deletions
+65
-39
core.js
src/core.js
+30
-19
event.js
src/event.js
+27
-17
core.js
test/unit/core.js
+8
-3
No files found.
src/core.js
View file @
9f8cd6c4
...
@@ -740,31 +740,42 @@ jQuery.extend({
...
@@ -740,31 +740,42 @@ jQuery.extend({
// A global GUID counter for objects
// A global GUID counter for objects
guid
:
1
,
guid
:
1
,
proxy
:
function
(
fn
,
proxy
,
thisObject
)
{
// Bind a function to a context, optionally partially applying any
if
(
arguments
.
length
===
2
)
{
// arguments.
if
(
typeof
proxy
===
"string"
)
{
proxy
:
function
(
fn
,
context
)
{
thisObject
=
fn
;
var
args
,
proxy
;
fn
=
thisObject
[
proxy
];
proxy
=
undefined
;
// Quick check to determine if target is callable, in the spec
// this throws a TypeError, but we will just return undefined.
}
else
if
(
proxy
&&
!
jQuery
.
isFunction
(
proxy
)
)
{
if
(
!
jQuery
.
isFunction
(
fn
)
)
{
thisObject
=
proxy
;
return
undefined
;
proxy
=
undefined
;
}
}
}
if
(
!
proxy
&&
fn
)
{
if
(
jQuery
.
isFunction
(
Function
.
prototype
.
bind
)
)
{
proxy
=
function
()
{
// Native bind
return
fn
.
apply
(
thisObject
||
this
,
arguments
);
args
=
slice
.
call
(
arguments
,
1
);
};
proxy
=
Function
.
prototype
.
bind
.
apply
(
fn
,
args
);
}
else
{
// Simulated bind
args
=
slice
.
call
(
arguments
,
2
);
if
(
args
.
length
)
{
proxy
=
function
()
{
return
arguments
.
length
?
fn
.
apply
(
context
,
args
.
concat
(
slice
.
call
(
arguments
)
)
)
:
fn
.
apply
(
context
,
args
);
};
}
else
{
proxy
=
function
()
{
return
arguments
.
length
?
fn
.
apply
(
context
,
arguments
)
:
fn
.
call
(
context
);
};
}
}
}
// Set the guid of unique handler to the same of original handler, so it can be removed
// Set the guid of unique handler to the same of original handler, so it can be removed
if
(
fn
)
{
proxy
.
guid
=
fn
.
guid
=
fn
.
guid
||
proxy
.
guid
||
jQuery
.
guid
++
;
proxy
.
guid
=
fn
.
guid
=
fn
.
guid
||
proxy
.
guid
||
jQuery
.
guid
++
;
}
// So proxy can be declared as an argument
return
proxy
;
return
proxy
;
},
},
...
...
src/event.js
View file @
9f8cd6c4
...
@@ -891,6 +891,8 @@ if ( document.addEventListener ) {
...
@@ -891,6 +891,8 @@ if ( document.addEventListener ) {
jQuery
.
each
([
"bind"
,
"one"
],
function
(
i
,
name
)
{
jQuery
.
each
([
"bind"
,
"one"
],
function
(
i
,
name
)
{
jQuery
.
fn
[
name
]
=
function
(
type
,
data
,
fn
)
{
jQuery
.
fn
[
name
]
=
function
(
type
,
data
,
fn
)
{
var
handler
;
// Handle object literals
// Handle object literals
if
(
typeof
type
===
"object"
)
{
if
(
typeof
type
===
"object"
)
{
for
(
var
key
in
type
)
{
for
(
var
key
in
type
)
{
...
@@ -904,10 +906,15 @@ jQuery.each(["bind", "one"], function( i, name ) {
...
@@ -904,10 +906,15 @@ jQuery.each(["bind", "one"], function( i, name ) {
data
=
undefined
;
data
=
undefined
;
}
}
var
handler
=
name
===
"one"
?
jQuery
.
proxy
(
fn
,
function
(
event
)
{
if
(
name
===
"one"
)
{
jQuery
(
this
).
unbind
(
event
,
handler
);
handler
=
function
(
event
)
{
return
fn
.
apply
(
this
,
arguments
);
jQuery
(
this
).
unbind
(
event
,
handler
);
})
:
fn
;
return
fn
.
apply
(
this
,
arguments
);
};
handler
.
guid
=
fn
.
guid
||
jQuery
.
guid
++
;
}
else
{
handler
=
fn
;
}
if
(
type
===
"unload"
&&
name
!==
"one"
)
{
if
(
type
===
"unload"
&&
name
!==
"one"
)
{
this
.
one
(
type
,
data
,
fn
);
this
.
one
(
type
,
data
,
fn
);
...
@@ -971,24 +978,27 @@ jQuery.fn.extend({
...
@@ -971,24 +978,27 @@ jQuery.fn.extend({
toggle
:
function
(
fn
)
{
toggle
:
function
(
fn
)
{
// Save reference to arguments for access in closure
// Save reference to arguments for access in closure
var
args
=
arguments
,
var
args
=
arguments
,
i
=
1
;
guid
=
fn
.
guid
||
jQuery
.
guid
++
,
i
=
0
,
toggler
=
function
(
event
)
{
// Figure out which function to execute
var
lastToggle
=
(
jQuery
.
data
(
this
,
"lastToggle"
+
fn
.
guid
)
||
0
)
%
i
;
jQuery
.
data
(
this
,
"lastToggle"
+
fn
.
guid
,
lastToggle
+
1
);
// Make sure that clicks stop
event
.
preventDefault
();
// and execute the function
return
args
[
lastToggle
].
apply
(
this
,
arguments
)
||
false
;
};
// link all the functions, so any of them can unbind this click handler
// link all the functions, so any of them can unbind this click handler
toggler
.
guid
=
guid
;
while
(
i
<
args
.
length
)
{
while
(
i
<
args
.
length
)
{
jQuery
.
proxy
(
fn
,
args
[
i
++
]
)
;
args
[
i
++
].
guid
=
guid
;
}
}
return
this
.
click
(
jQuery
.
proxy
(
fn
,
function
(
event
)
{
return
this
.
click
(
toggler
);
// Figure out which function to execute
var
lastToggle
=
(
jQuery
.
data
(
this
,
"lastToggle"
+
fn
.
guid
)
||
0
)
%
i
;
jQuery
.
data
(
this
,
"lastToggle"
+
fn
.
guid
,
lastToggle
+
1
);
// Make sure that clicks stop
event
.
preventDefault
();
// and execute the function
return
args
[
lastToggle
].
apply
(
this
,
arguments
)
||
false
;
}));
},
},
hover
:
function
(
fnOver
,
fnOut
)
{
hover
:
function
(
fnOver
,
fnOut
)
{
...
...
test/unit/core.js
View file @
9f8cd6c4
...
@@ -858,7 +858,7 @@ test("jQuery.isEmptyObject", function(){
...
@@ -858,7 +858,7 @@ test("jQuery.isEmptyObject", function(){
});
});
test
(
"jQuery.proxy"
,
function
(){
test
(
"jQuery.proxy"
,
function
(){
expect
(
4
);
expect
(
5
);
var
test
=
function
(){
equals
(
this
,
thisObject
,
"Make sure that scope is set properly."
);
};
var
test
=
function
(){
equals
(
this
,
thisObject
,
"Make sure that scope is set properly."
);
};
var
thisObject
=
{
foo
:
"bar"
,
method
:
test
};
var
thisObject
=
{
foo
:
"bar"
,
method
:
test
};
...
@@ -872,8 +872,13 @@ test("jQuery.proxy", function(){
...
@@ -872,8 +872,13 @@ test("jQuery.proxy", function(){
// Make sure it doesn't freak out
// Make sure it doesn't freak out
equals
(
jQuery
.
proxy
(
null
,
thisObject
),
undefined
,
"Make sure no function was returned."
);
equals
(
jQuery
.
proxy
(
null
,
thisObject
),
undefined
,
"Make sure no function was returned."
);
// Use the string shortcut
// Partial application
jQuery
.
proxy
(
thisObject
,
"method"
)();
var
test2
=
function
(
a
){
equals
(
a
,
"pre-applied"
,
"Ensure arguments can be pre-applied."
);
};
jQuery
.
proxy
(
test2
,
null
,
"pre-applied"
)();
// Partial application w/ normal arguments
var
test3
=
function
(
a
,
b
){
equals
(
b
,
"normal"
,
"Ensure arguments can be pre-applied and passed as usual."
);
};
jQuery
.
proxy
(
test3
,
null
,
"pre-applied"
)(
"normal"
);
});
});
test
(
"jQuery.parseJSON"
,
function
(){
test
(
"jQuery.parseJSON"
,
function
(){
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment