Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17670
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2012-12-10 23:46 -0800 |
| References | <1892433a-7696-4b03-a94c-0b1f0a984134@googlegroups.com> <XnsA121943364577jaejunksglegooilma@0.0.0.23> <cfb82688-a712-4e52-9ec3-ed7f0697320c@googlegroups.com> <XnsA122C28BCCF7Fjaejunksglegooilma@0.0.0.23> |
| Message-ID | <587e3d40-5635-4e67-b7be-b777744f51c1@googlegroups.com> (permalink) |
| Subject | Re: Cheap runtime asserts |
| From | glathoud <glathoud@yahoo.fr> |
On Friday, December 7, 2012 1:03:08 PM UTC+1, JJ wrote:
> But how to do it if a parameter of null and non-null are both accepted by
>
> a function? For example, a function to toggle (set/clear) an element's
>
> onclick handler.
>
>
>
> function toggleHandler(ele, func) {
>
> ele.onclick.a;
>
> func.a;
>
> ele.onclick = ele.onclick ? null : func;
>
> }
In such a case I would probably not use the approach for `func`. Just check `ele`.
I wrote up a bit more about the background leading to the approach: http://glat.info/js.cheap-asserts/
In general it is up to you to decide, for each parameter, whether to "cheap-check", to fully check (like Thomas described) or to leave it as is (e.g. when expecting multiple types and/or using coercion). In the end, this is a matter a cost/benefit analysis.
But in this particular case, you could do this:
function toggleHandler(ele, func) {
ele.onclick.a;
func == null || func.call.a;
ele.onclick = ele.onclick ? null : func;
}
`func == null` covers both `null` and `undefined` cases.
(and using `func.call.a` is a bit more specific to functions than `func.a`).
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-04 22:00 -0800
Re: Cheap runtime asserts Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-05 20:18 +0100
Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-06 11:19 -0800
Re: Cheap runtime asserts Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-06 23:41 +0100
Re: Cheap runtime asserts Patricia Shanahan <pats@acm.org> - 2012-12-05 12:00 -0800
Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-06 11:34 -0800
Re: Cheap runtime asserts JJ <jaejunks@glegooilma-swapit.com> - 2012-12-06 07:29 +0000
Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-06 11:40 -0800
Re: Cheap runtime asserts JJ <jaejunks@glegooilma-swapit.com> - 2012-12-07 12:03 +0000
Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-10 23:46 -0800
Re: Cheap runtime asserts Stefan Weiss <krewecherl@gmail.com> - 2012-12-06 23:00 +0100
Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-11 00:07 -0800
csiph-web