Tuesday, June 10, 2014

Javascript hoistology



Scope and Hoisting explained
  • Unlike most programming languages, JavaScript does not have block-level scope
  • Variables have either a local scope or a global scope.
  • If you don't declare your local variables with the var keyword, they are part of the global scope
  • All variables declared outside a function are in the global scope. In the browser, which is what we are concerned with as front-end developers, the global context or scope is the window object
  • a variable is initialized (assigned a value) without first being declared with the var keyword, it is automatically added to the global context and it is thus a global variable
Only functions create new scope
http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

JavaScript Declarations are Hoisted - variable used before its declared. JavaScript Initializations are Not Hoisted -  http://www.w3schools.com/js/tryit.asp?filename=tryjs_hoisting1


http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/
If a value is enumerable, it will show up when enumerating over an object using a for(prop in obj) loop. If a property is writable, you can replace it. If a property is configurable, you can delete it or change its other attributes. 
var person = Object.create(null); 
Object.defineProperty(person, 'firstName', { value: "Yehuda", writable: true, enumerable: true, configurable: true });



hasownproperty-vs-propertyisenumerable
The propertyIsEnumerable method returns true if proName exists in object and can be enumerated using a ForIn loop. The propertyIsEnumerable method returns false if object does not have a property of the specified name or if the specified property is not enumerable. Typically, predefined properties are not enumerable while user-defined properties are always enumerable. The hasOwnProperty method returns true if object has a property of the specified name, false if it does not. This method does not check if the property exists in the object's prototype chain; the property must be a member of the object itself.

Every function has these methods you can call (Object.getOwnPropertyNames(Function.prototype)):
  • toString
  • call
  • apply
  • length
  • name
  • arguments
  • caller
  • constructor
  • bind

http://www.codelord.net/2014/03/14/please-use-hasownproperty-short-story-of-a-hack/



http://www.akhildeshpande.com/2012/09/javascript-prototype.html#more


No comments:

Post a Comment