Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F7539034
test_native_bind.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
test_native_bind.js
View Options
// Test binding native event functions
console
.
log
(
'Testing .bind() with native functions...\n'
);
// Test 1: Bind addEventListener
console
.
log
(
'Test 1: Bind addEventListener'
);
const
target
=
createEventTarget
();
const
boundAdd
=
target
.
addEventListener
.
bind
(
target
);
console
.
log
(
' boundAdd type:'
,
typeof
boundAdd
);
try
{
boundAdd
(
'test'
,
()
=>
{
console
.
log
(
' Listener called!'
);
});
target
.
dispatchEvent
(
'test'
);
console
.
log
(
' PASS: Bound native function works!'
);
}
catch
(
e
)
{
console
.
log
(
' FAIL:'
,
e
);
}
// Test 2: Store bound method in object
console
.
log
(
'\nTest 2: Store bound methods in object'
);
const
target2
=
createEventTarget
();
const
wrapper
=
{};
wrapper
.
on
=
target2
.
addEventListener
.
bind
(
target2
);
wrapper
.
emit
=
target2
.
dispatchEvent
.
bind
(
target2
);
try
{
wrapper
.
on
(
'test'
,
()
=>
{
console
.
log
(
' Stored bound listener called!'
);
});
wrapper
.
emit
(
'test'
);
console
.
log
(
' PASS: Stored bound methods work!'
);
}
catch
(
e
)
{
console
.
log
(
' FAIL:'
,
e
);
}
// Test 3: Use in a class
console
.
log
(
'\nTest 3: Use bound methods in class'
);
class
MyEmitter
{
constructor
()
{
const
target
=
createEventTarget
();
this
.
on
=
target
.
addEventListener
.
bind
(
target
);
this
.
emit
=
target
.
dispatchEvent
.
bind
(
target
);
}
}
try
{
const
emitter
=
new
MyEmitter
();
emitter
.
on
(
'test'
,
()
=>
{
console
.
log
(
' Class listener called!'
);
});
emitter
.
emit
(
'test'
);
console
.
log
(
' PASS: Class with bound methods works!'
);
}
catch
(
e
)
{
console
.
log
(
' FAIL:'
,
e
);
}
console
.
log
(
'\nAll tests completed!'
);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Jun 17, 1:11 PM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557510
Default Alt Text
test_native_bind.js (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment