Javascript Notes
What This Post Is
Rather than a more standard format blog post, this post will be more of an aggregation of things I've learned/re-learned about javascript and how it works. Intended to be a reference and also somewhere to cement ideas so hopefully they'll stick in my head better.
Terms and Definitions
Enumerable properties
JS object properties all contain an internal flag indicating whether or not the property is enumerable. Default assignment or initialization set this flag to True. Using defineProperty() sets this flag to False. Enumerable properties are accessible via functionality ( Object.keys(), for...in, etc. )
Why would you want to have a property NOT be iterable?
- Clean serialization (non enumerable properties are not serialized.
- Protect metadata properties from being modified
Reference(s): MDN Docs
Prototype
Object prototypes are the method by which objects inherit from one another. All objects have a built in prototype property. By convention, the property is called __proto__. That prototype property can have its own prototype property (the prototype chain) that continues until the resulting prototype object's own prototype property is null. When accessing an object's properties, they are searched for in a hierarchical structure- first, the object's direct properties are searched. If not found, the prototype properties are searched- all the way up until the property is found or we hit the null prototype.
'Use Strict'
Enabling strict mode in javascript enforces stricter parsing and error handling. Things that might fly with lax javascript will now get you in trouble. For example:
- Global variables. Assigning a value to an undeclared variable throws a
ReferenceErrorinstead of creating it. - Creating a function that has params with the same names is not allowed.
- Attempting to modify a 'read-only' attribute on an object will actually throw an error rather than fail silently
thisis no longer automatically bound to the global object when invoking a function without a specified object
'use strict'
function foo() {
console.log(this); //undefined
}
foo();
Comments
No comments yet.
Leave a comment