Thursday 15 March 2012

syntax - Javascript - this vs window -



syntax - Javascript - this vs window -

if can, if code not invoked in function, should utilize instead of window?

this.addeventlistener(...); vs window.addeventlistener(...);

this 2 chars shorter, improve utilize if can?

i recommend avoid 'this' unless explicitly specified via call, apply, or bind. way there's never question this refers to. , i'm fond of explicitly declaring access global object:

(function(global, document, undefined){ global.myglobalvar = 'foo'; })(window, document);

this way clear when i'm doing global, code can more ported non-browser environment node.js, undefined undefined, etc.

if don't create global access explicit , have seek find later, will regret it. if you're registering event handler globally (probably not best idea) want create note of it.

edit: should mention in strict mode this undefined in global scope.

javascript syntax

No comments:

Post a Comment