SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Jul 11, 2008, 12:20 #1
- Join Date
- May 2008
- Posts
- 5
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Javascript - Shortcut Coding Technique?
I'm pretty intermediate at Javascript and recently was editing some facebook apps and came along this bit of script that does some sort of coding shortcut (I think).
Code JavaScript:function typeahead(obj, options, option_ids, doc_location) { ... // Setup the events we're listening to this.obj.purgeEventListeners('focus') .addEventListener('focus', this.onfocus.bind(this)) .addEventListener('blur', this.onblur.bind(this)) .addEventListener('keyup', this.onkeyup.bind(this)) .addEventListener('keydown', this.onkeydown.bind(this)) .addEventListener('keypress', this.onkeypress.bind(this)); ... }
Does Javascript automatically know to prepend this.obj before each addEventListener? Or is something going on here?Last edited by Dental Floss; Jul 11, 2008 at 12:56.
-
Jul 11, 2008, 13:05 #2
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No, you're being tricked by the indention. It's all a single statement like this,
Code:this.x.p().a().a().a().a().a()
Birnam wood is come to Dunsinane
-
Jul 11, 2008, 13:38 #3
- Join Date
- Dec 2007
- Posts
- 207
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A with statement comes to mind.
Not that it should be used though.mmj
-
Jul 11, 2008, 16:32 #4
- Join Date
- May 2008
- Posts
- 5
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is what I have heard from others as well. Man I hate javascript
As for them being all one line, I thought javascript didn't require a semicolon to be a valid statement, just a new line character. Taken from the ECMA-262.pdf that defines this crap, semicolons are added when:
1. The offending token is separated from the previous token by at least one LineTerminator.
2. The offending token is }.
And looking at Mozilla's addeventlistener definition, I don't see a return value: http colon slash slash developer.mozilla.org/en/docs/DOM:element.addEventListener
-
Jul 11, 2008, 21:45 #5
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Automatic semicolon insertion is complicated. In this case there is no offending token, so no semicolons will be inserted.
Birnam wood is come to Dunsinane
Bookmarks