Skip to content

Instantly share code, notes, and snippets.

@blaja
blaja / .js
Created August 7, 2015 01:58
This is how to extend Host(DOM,BOM) objects. But best to not do it at all, use wrappers.
const $ = function(selector) { return document.querySelector(selector); }; // Returns first matched element or null
const $$ = function(selector) { return document.querySelectorAll(selector); }; // Returns NodeList of matched elements or empty NodeList []
// If you want to extend HTML DOM prototype do it this way or something like this.
// Check for property or method pre-existance. Obv if JS Engine already provides property or method, you don't want to override it with your own custom one.
// This works in IE9+ or can be done even in IE8+
// Adding a new method to all HTML elements via the HTMLElement prototype
if(!HTMLElement.prototype.remove) {
// defineProperty because of enumeration
@blaja
blaja / .js
Created August 8, 2015 13:41
Inspecting internal [[class]] of objects in JS
// NOTE: this is specification dependent classification. It has nothing to do with formal class programming concept.
// The value of the [[Class]] internal property is defined by specification for every kind of built-in object.
// The value of the [[Class]] internal property of a host object may be any String value except one of:
// "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String".
// The value of a [[Class]] internal property is used internally to distinguish different kinds of objects.
Object.prototype.toString.call({}); // "[object Object]"
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call(function(){}); // "[object Function]"
Object.prototype.toString.call(''); // "[object String]"
@blaja
blaja / .js
Created August 5, 2015 10:35
Get for..of loop on NodeList interface in Chrome
NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];