- Private variable using closures
function X () { id = 0; ID+ +; } } var makeid = x (); var i = Makeid (); var j = Makeid ();
id
Have effectively private access, via (and only) via the closure Makeid () function.
- Remembering variables via closures
Foo () { req = XMLHTTP (); Req.onreadystatechange = function () {/*closure created here*/ if (req. readyState = 4) { ... } } }
Even though onreadystatechange
the is a property/method of req, it's not invoked *via* req by the request handling (browser) thread. Since T needs access to itself and can either use a global variable (essentially window.req) or a closure to Ho Ld/access a reference to it.See Also:mozilla Developer Docs
- Closures has access to variables the can be changed
x = elements (' a ') for (var i = 0; i < i++) X[i].onclick = function () {/*...use x[i] here...*/} }
All onclick
functions would refer to x[10], the final/latest value of the the the Closed variable i
(the loop counter variabl E is i
a closed variable for the inner function within the loop).
- Various Ways for Object creation
A) New and function definition combined var obj = new function () {/* stuff */} {}
- Defining Object Methods
MYOBJ () {}Myobj.prototype = { foo: function () { alert (this); } ... etc.. } var my1 = new MyObj (); my1. Foo ();
- toggling Display Visibility
var elem = getelement (' elem '); elem.style.display = ';
Usedisplay: ‘‘
As opposed todisplay: block
(This uses the default "none" of type which can differ from block or table-row or whatever, but in all cases, makes the Eleme NT visible properly.However, setting only elem.style.display = ‘‘
Sets or removes the inline style. If a document level CSS style declaration also exists and applies to this element, then once the inline style is removed, the element would be still is rendered with the document level declaration (since have elem.style.display
effectively removed th e higher precedence inline style, but the document level declaration is unaffected and would now apply). So either:
- Don ' t use both non-inline styles and inline styles when toggling display property via
style.display = ...
Line styles)
- Or, change the Stylesheet/classname as well as the display property if using classname styles or both inline/non- Inline at the same time.
See:stack Overflow Thread
- Changing CSS for an element
Only changes/modifies inline styleelem.style.xxx = ...//classnameelem.classname = ...
Either inline styles or className can be used.
- Use className
elem.classname = ' Foo ';
Use and not className
class
(since class can is a reserved word).
- Use CSSFloat
Elem.cssfloat = ' Left ';
Use cssFloat
of the CSS float property and not float
, since float is a reserved word in JS.
- use Classlist when elements had multiple classesfor multiple classes, one had to is careful to parse/save the class s Tring properly, for example:
elem.classname = ' foo bar baz ';
using Classlist makes working with multiple names easier, with the add
, & nbsp remove
and Toggle
methods. var cl = elem.classlist;cl.add (' foo '); Cl.toggle ("Bar");
See: mozilla Docs
- Ways to invoke JS event handlers
-in html<a href= "Javascript:foo ()" <anyelement onclick= "Javascript:foo ()" <anyelement onclick= "foo ()" ( The "javascript:" is optional) <form action= "foo ()" (the "javascript:" is optional)-in JS Codeanyelement.onclick = Foo
Don ' t return a value in onclick event handlers for HREF's etc, (return void 0
or undefined
instead) because in some cases, the Browser would show the return value instead (as if the user navigated to a new page with that value)More Reading:quirksmode
- DOM 0 Event handlers is methods
<body><form><button type= ' button ' onclick= "MyFunc (This, ' Button.onclick ', event)" >my button</ Button></form><div onclick= "MyFunc (This, ' Div.onclick ')" >my div</button><script> function MyFunc (Elem, MSG, e) {Console.log (Elem, MSG, e);} </script></body>
(as a small aside, a button inside a form submits that form unless a type=button is specified, the vagaries of legacy HTML )The event
object is a available in inline handlers via the ' event ' object. If applicable handlers is declared on the parent elements, then as events bubble up to parent, parent handlers would be invoke D for the parent, again (with ' this ' being the parent, the parent handler is invoked). event.target
the always remains the original element upon which received the initial event (click, etc).
If this
is isn't passsed in the Inline-handler, this refers to the window where the script was running, in the event handler function itself.
Event handlers declared in JSbutton.onclick = ...<.code> are methods of the corresponding DOM button object, in which the this
object is automatically set to the element on which the event was invoked.
Further reading see: quirksmode, another great tutorial