Prev: using screenX and screenY?
Next: ECMA-262-5 in detail. Chapter 1. Properties and Property Descriptors.
From: Garrett Smith on 29 Apr 2010 15:16 Thomas 'PointedEars' Lahn wrote: > Garrett Smith wrote: > >> A safe way to remove an existing event handler property is to replace >> the value with a function the does nothing and returns undefined. The >> global noop function `Function.prototype` fits that bill perfectly. >> >> Example: >> hostObject[methodName] = Function.prototype; > ECMA-262 states: | The Function prototype object is itself a Function object (its | [[Class]] is "Function") that, when invoked, accepts any arguments | and returns undefined. Function.prototype returns nothing. Implementation of Function.prototype that fulfills that could only be unsafe if it were to add some side effects. The same could be said for String, or any other function. Such side effects would be taking on additional nonstandard functionality. Calling Function.prototype with the expectation that it has no observable side effects is as safe as calling String, parseInt, isNaN with the same expectation. Function.prototype does nothing and returns nothing. When the program wants a function that does nothing and returns nothing, Function.prototype is a good candidate for that. var noop = Function.prototype; // Reusable everywhere. One case where Function.prototype function cannot be used is for new expressions; Function.prototype is not specified to implement [[Construct]], and so a TypeError can be expected. It is unsafe to use Function.prototype function in reusable constructor patterns such as those used for prototype inheritance. var f = Function.prototype; f.prototype = MyCtor.prototype; var i = new f; // TypeError -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Thomas 'PointedEars' Lahn on 29 Apr 2010 16:30 Garrett Smith wrote: > Thomas 'PointedEars' Lahn wrote: >> Garrett Smith wrote: >>> A safe way to remove an existing event handler property is to replace >>> the value with a function the does nothing and returns undefined. The >>> global noop function `Function.prototype` fits that bill perfectly. >>> >>> Example: >>> hostObject[methodName] = Function.prototype; > > ECMA-262 states: > > | The Function prototype object is itself a Function object (its > | [[Class]] is "Function") that, when invoked, accepts any arguments > | and returns undefined. We've been over this. > Function.prototype returns nothing. Irrelevant. > Implementation of Function.prototype that fulfills that could only be > unsafe if it were to add some side effects. Which is not forbidden. > The same could be said for String, or any other function. No, those are fully specified. > Such side effects would be taking on additional nonstandard > functionality. Calling Function.prototype with the expectation that it > has no observable side effects is as safe as calling String, parseInt, > isNaN with the same expectation. Fallacy. > Function.prototype does nothing and returns nothing. Wishful thinking, nothing based on fact. > When the program wants a function that does nothing and returns nothing, > Function.prototype is a good candidate for that. Fallacy. > var noop = Function.prototype; // Reusable everywhere. No. > One case where Function.prototype function cannot be used is for new > expressions; Function.prototype is not specified to implement > [[Construct]], Yes, it is. All "Function objects" must have that property, per section 13.2. > and so a TypeError can be expected. Wrong. > It is unsafe to use Function.prototype function in reusable constructor > patterns such as those used for prototype inheritance. It should not be, side effects aside. > var f = Function.prototype; > f.prototype = MyCtor.prototype; > var i = new f; // TypeError You would have found a bug, then. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Lasse Reichstein Nielsen on 29 Apr 2010 16:59 Garrett Smith <dhtmlkitchen(a)gmail.com> writes: > ECMA-262 states: > > | The Function prototype object is itself a Function object (its > | [[Class]] is "Function") that, when invoked, accepts any arguments > | and returns undefined. > > Function.prototype returns nothing. Uhm, why then does your quote say "... and returns undefined."? Unless undefined is nothing, but why have a second name for undefined. /L -- Lasse Reichstein Holst Nielsen 'Javascript frameworks is a disruptive technology'
From: Ry Nohryb on 29 Apr 2010 17:12 On Apr 29, 10:30 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > Garrett Smith wrote: > > Thomas 'PointedEars' Lahn wrote: > >> Garrett Smith wrote: > > > One case where Function.prototype function cannot be used is for new > > expressions; Function.prototype is not specified to implement > > [[Construct]], > > Yes, it is. All "Function objects" must have that property, per section > 13.2. > > > and so a TypeError can be expected. > > Wrong. > > > It is unsafe to use Function.prototype function in reusable constructor > > patterns such as those used for prototype inheritance. > > It should not be, side effects aside. > > > var f = Function.prototype; > > f.prototype = MyCtor.prototype; > > var i = new f; // TypeError > > You would have found a bug, then. new (new Date().getTime) TypeError: Result of expression '(new Date().getTime)' [function getTime() { [native code] }] is not a constructor. new ({}).toString TypeError: Result of expression '({}).toString' [function toString() { [native code] }] is not a constructor. new Function.prototype TypeError: Result of expression 'Function.prototype' [function () { [native code] }] is not a constructor. etc etc. -- Jorge.
From: Garrett Smith on 29 Apr 2010 18:38
Lasse Reichstein Nielsen wrote: > Garrett Smith <dhtmlkitchen(a)gmail.com> writes: > >> ECMA-262 states: >> >> | The Function prototype object is itself a Function object (its >> | [[Class]] is "Function") that, when invoked, accepts any arguments >> | and returns undefined. >> >> Function.prototype returns nothing. > > Uhm, why then does your quote say "... and returns undefined."? > Unless undefined is nothing, but why have a second name for undefined. > No good reason really. Better to call things what they really are. -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/ |