473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Prototype: bind() vs bindAsEventListener()

Can anyone tell me what the difference is between these two protoype
methods?
They are extensions of Javascript's Function class.

Is bindAsEventListener just used to be compatible with IE's Event
model?
I'm thinking the only difference between the 2 methods is that
bindAsEventListener passes the event into the function.

I want to know how I can practically apply these.

METHODS FROM prototype-1.5.0_rc1.js
Function.prototype.bind = function() {
var __method = this, args = $A(arguments), object = args.shift();
return function() {
return __method.apply(object, args.concat($A(arguments)));
}
}

Function.prototype.bindAsEventListener = function(object) {
var __method = this, args = $A(arguments), object = args.shift();
return function(event) {
return __method.apply(object, [( event ||
window.event)].concat(args).concat($A(arguments)));
}
}

Jan 15 '07 #1
4 10352
VK

danf wrote:
Can anyone tell me what the difference is between these two protoype
methods?
I don't know, but this may help:

<http://wiki.script.aculo.us/scriptaculous/show/Function.prototype.bind>
<http://wiki.script.aculo.us/scriptaculous/show/Function.prototype.bindAsEventListener>

Jan 15 '07 #2
danf wrote:
Can anyone tell me what the difference is between these two protoype
methods?
If you are absolutely stuck using Prototype and can't escape it then
the best place for Prototype-specific questions is the Rails spinoffs
list. It is the de facto Prototype support list.

<URL: http://groups.google.com/group/rubyonrails-spinoffs>

Peter

Jan 15 '07 #3
danf wrote:
Can anyone tell me what the difference is between these two protoype
methods?
They are extensions of Javascript's Function class.
Roughly, the only different is that bindAsEventListener sets the first
argument of the function (arguments[0]) as the event objet. But also,
it ensures that event is consistent in both IE and firefox. Meaning
that you won't need to add this line : event = event || window.event,
at the beginning of you event function. So, yes, it is cross-browser
compatible.

Consider this :

function clickEvent(arg1, arg2) {
alert( "this = " + this + "\narg1 = " + arg1 + "\narg2 = " + arg2 );
}

var element1 = $('someElement1');
// this = inconsistent, arg1 = inconsistent, arg2 = undefined
element1.onclick = clickEvent;

var element2 = $('someElement2');
// this = element2, arg1 = 1, arg2 = 2
element2.onclick = clickEvent.bind(element2, 1, 2);

var element3 = $('someElement3');
// this = element3, arg1 = event, arg2 = 1
element3.onclick = clickEvent.bindAsEventListener(element3, 1, 2);

Is bindAsEventListener just used to be compatible with IE's Event
model?
As you see, bind and bindAsEventListener may be applied to any DOM
event model as they ensure a consistency in most implementations.
I'm thinking the only difference between the 2 methods is that
bindAsEventListener passes the event into the function.
Yes, that's the main thing, but as I said, bindAsEventListener's first
parameter will always be the event object in most browsers (meaning
that the only browsers making exception here are barely used by less
than 8% of the population, and in most case, they don't even implement
proper JS, CSS, XML, etc. see
http://www.w3schools.com/browsers/browsers_stats.asp)
I suggest that you read about function binding to have a better
understanding of what these two functions actually do in your code. A
good start is here :
http://developer.mozilla.org/en/docs...:this_Operator

Jan 16 '07 #4
Thanks for the response - and the link was a great resource.

Yanick wrote:
danf wrote:
Can anyone tell me what the difference is between these two protoype
methods?
They are extensions of Javascript's Function class.

Roughly, the only different is that bindAsEventListener sets the first
argument of the function (arguments[0]) as the event objet. But also,
it ensures that event is consistent in both IE and firefox. Meaning
that you won't need to add this line : event = event || window.event,
at the beginning of you event function. So, yes, it is cross-browser
compatible.

Consider this :

function clickEvent(arg1, arg2) {
alert( "this = " + this + "\narg1 = " + arg1 + "\narg2 = " + arg2 );
}

var element1 = $('someElement1');
// this = inconsistent, arg1 = inconsistent, arg2 = undefined
element1.onclick = clickEvent;

var element2 = $('someElement2');
// this = element2, arg1 = 1, arg2 = 2
element2.onclick = clickEvent.bind(element2, 1, 2);

var element3 = $('someElement3');
// this = element3, arg1 = event, arg2 = 1
element3.onclick = clickEvent.bindAsEventListener(element3, 1, 2);

Is bindAsEventListener just used to be compatible with IE's Event
model?

As you see, bind and bindAsEventListener may be applied to any DOM
event model as they ensure a consistency in most implementations.
I'm thinking the only difference between the 2 methods is that
bindAsEventListener passes the event into the function.

Yes, that's the main thing, but as I said, bindAsEventListener's first
parameter will always be the event object in most browsers (meaning
that the only browsers making exception here are barely used by less
than 8% of the population, and in most case, they don't even implement
proper JS, CSS, XML, etc. see
http://www.w3schools.com/browsers/browsers_stats.asp)
I suggest that you read about function binding to have a better
understanding of what these two functions actually do in your code. A
good start is here :
http://developer.mozilla.org/en/docs...:this_Operator
Jan 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the...
12
by: petermichaux | last post by:
Hi, I've been reading the recent posts and older archives of comp.lang.javascript and am surprised by the sentiments expressed about the prototype.js library for a few reasons: 1) The library...
28
by: Diodeus | last post by:
I would like to set up an event observer outside of an object, so I can't use this.bindAsEventListener. How can I pass the correct object reference? I tried something like this, and various...
1
by: tdan | last post by:
I do not know how to get Event.stopObserving() to work in the context I am using it. I am displaying a Color Selection Table and attaching 2 events: 1. onmouseover to display the color to the user...
1
by: fgualda87 | last post by:
Hey I'm using this tooltip which uses prototype 1.6.1.js /* * Copyright (c) 2006 Jonathan Weiss <jw@innerewut.de> * * Permission to use, copy, modify, and distribute this software for any ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.