#invoke to support host objects' methods
Reported by Juriy Zaytsev | August 13th, 2008 @ 05:32 PM | in 1.6.1
As per http://groups.google.com/group/c...
we could change Array#invoke to support host objects' methods - the ones that might not have call or apply:
invoke: function(method) {
var args = $A(arguments).slice(1);
return this.map(function(value) {
return value[method].apply
? value[method].apply(value, args)
: Function.prototype.apply.call(value[method], value, args);
})
}
Does this seem reasonable?
Comments and changes to this ticket
-
Juriy Zaytsev August 13th, 2008 @ 05:34 PM
- → Tag changed from to ie needs_patch needs_tests
- → State changed from new to enhancement
-
John-David Dalton August 13th, 2008 @ 05:36 PM
I am totally claiming an assist on this one :D + 1 + 1
-
John-David Dalton August 13th, 2008 @ 05:37 PM
What about just doing
invoke: function(method) { var args = $A(arguments).slice(1); return this.map(function(value) { return Function.prototype.apply.call(value[method], value, args); }); } -
-
John-David Dalton August 17th, 2008 @ 04:24 AM
Even better:
invoke: function(method) { var args = Array.prototype.slice.call(arguments, 1); return this.map(function(value) { return Function.prototype.apply.call(value[method], value, args); }); } -
Juriy Zaytsev August 17th, 2008 @ 06:32 PM
@John
Well, that's another story.
If we replace "$A(...).slice(...)" with a ".call", then it should probably be done consistently throughout the library.
-
John-David Dalton August 17th, 2008 @ 06:55 PM
In this case it makes more sense to do it because its already using slice. So even if the rest of the library isn't changed it still makes sense.
-
Andrew Dupont October 4th, 2008 @ 05:48 AM
- → Assigned user changed from to Juriy Zaytsev
-
Juriy Zaytsev October 6th, 2008 @ 07:45 PM
It looks like certain methods can not be invoked even using this workaround.
focusfails:elements = [ new Element('input', { type: 'text' }) ]; this.assert(Object.isArray(elements.invoke('focus'))); Error: "Unexpected call to method or property access"while
getAttributeworks:var elements = [ new Element('div', { title: 'foo' }), new Element('span', { title: 'bar' }), new Element('a', { title: 'baz' }) ]; this.assertEnumEqual(['foo', 'bar', 'baz'], elements.invoke('getAttribute', 'title')); -
Andrew Dupont October 6th, 2008 @ 07:54 PM
Looks like there are two issues here: (a) calling a function in an arbitrary scope; and (b) calling a function via bracket notation.
For instance, just because you can do
Function.prototype.apply.call(foo.getAttribute, foo, "bar");doesn't mean you can necessarily do
foo['getAttribute']("bar");Perhaps that's the problem.
-
Juriy Zaytsev October 6th, 2008 @ 10:02 PM
My bad :/
focus/blurthrow errors in IE when called on elements not inserted into a document.Function.prototype.applyworks as expected when form controls are appended to DOM.Attached are patch and tests.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
The Prototype JavaScript library.
