Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
@WebReflection
WebReflection / O.js
Last active August 29, 2015 13:57
if the only reason you like __proto__ is its literal integration, just use this and make the web better today. No credits needed
// ES5 dirty perf oriented tweet size
var O=function(O,s){s=O.setPrototypeOf||function(o,p){o.__proto__=p;return o};return function(p,o){return o?s(o,p):p||s({},p)}}(Object);
// ES5 dirty performance way
var O = function(Object){
var
create = Object.create,
set = Object.setPrototypeOf || function(o, p) {
o.__proto__ = p;
return o;
@WebReflection
WebReflection / handleEvent.md
Last active December 25, 2025 22:31
because handleEvent is the most powerful way to deal with DOM events or states (and not only DOM)

Update

This snippet is now improved and officially an npm module called dom-handler

@WebReflection
WebReflection / examples.md
Last active August 29, 2015 13:57
some example about define-strict-properties - https://github.com/WebReflection/define-strict-properties

All code assumes define strict properties in included upfront.

However, being the proposal fullyt backward compatible, you can tests these snipets by copy and paste but no type will be guarded.

Point2D Class

function Point2D(x, y) {
  this.x = x;
  this.y = y;
}
@WebReflection
WebReflection / node.md
Created March 18, 2014 20:41
node environment

global

decodeURI
Float64Array
URIError
clearTimeout
setInterval
RangeError
escape
RegExp
@WebReflection
WebReflection / nashorn.md
Last active May 19, 2018 10:08
nashorn environment

global

parseInt
parseFloat
isNaN
isFinite
encodeURI
encodeURIComponent
decodeURI
decodeURIComponent
@WebReflection
WebReflection / JS2.md
Last active August 29, 2015 13:57
used to generate an overview of a generic JavaScript environment

Generates a Markdown friendly output similar to the one showed here.

(function show(object, name, all) {
  var f, k, later = [], list = ['### ' + name, '```javascript'];
  all.push(object);
  Object.getOwnPropertyNames(object).forEach(function(name) {
    list.push(name);
    if (/^[A-Z]/.test(name) && object[name] instanceof Object) {
 later.push([object, name]);
@WebReflection
WebReflection / createEmitter.js
Created March 5, 2014 23:39
Simplified EventEmitter like objects
function createEmitter() {
// not ideal when many simple emitters are needed
// returns object similar to node.js EventEmitter API
// not compatible with DOM nodes, only JS controllers
var
// on, off, emit as immutable and non enumerable
// all methods returns the implicitly bound emitter
emitter = Object.create(null, {
on: {value: function (type, handler) {
var tmp = get(type);
@WebReflection
WebReflection / Object.getOwnPropertyDescriptors.md
Created March 4, 2014 19:28
Object.getOwnPropertyDescriptors(O)

Object.getOwnPropertyDescriptors(O)

When the getOwnPropertyDescriptors function is called, the following steps are taken:

  1. Let obj be ToObject(O).
  2. ReturnIfAbrupt(obj).
  3. Let keys be the result of calling the [[OwnPropertyKeys]] internal method of obj.
  4. ReturnIfAbrupt(keys).
  5. Let descriptors be the result of the abstract operation ObjectCreate with the intrinsic object %ObjectPrototype% as its argument.
  6. Let gotAllNames be false.
@WebReflection
WebReflection / Object.getOwnPropertyDescriptors.js
Created March 3, 2014 01:39
A plural ES5 + ES6 friendly version of Object.getOwnPropertyDescriptor
'getOwnPropertyDescriptors' in Object || (
Object.getOwnPropertyDescriptors = function (Object) {
var
gOPD = Object.getOwnPropertyDescriptor,
gOPN = Object.getOwnPropertyNames,
gOPS = Object.getOwnPropertySymbols,
gNS = gOPS ? function (object) {
return gOPN(object).concat(gOPS(object));
} :
gOPN,
@WebReflection
WebReflection / NamedStorage.md
Last active August 29, 2015 13:56
DOM NamedStorage Interface Proposal with polyfills for ES3, ES5, and ES6

DOM NamedStorage Interface Proposal

Multiple scripts and multiple libraries have usually a common need of the W3C Web Storage, either the local or the session one.

However, it is extremely easy and very hard to prevent a not so infrequent storage.clear() call, able to remove any sort of data and for any kind of lbirary in the same realm.

A Possible Solution

This proposal aim is to harmonize multiple libraries preserving same interface but enriching the constructor.

Please note that current WebIDL is almost identical to the one already recommended by W3C.