Skip to content

Instantly share code, notes, and snippets.

View juandopazo's full-sized avatar

Juan Dopazo juandopazo

View GitHub Profile
@juandopazo
juandopazo / gist:1004493
Created June 2, 2011 13:59
Quick note about objects as YUI3 attributes default values
/**
* If you want to set the default value of an attribute to be an array or object,
* doing this fails:
*/
var MyClass = Y.Base.create('myClass', Y.Base, [], {}, {
ATTRS: {
myAttr: { value: [] }
}
});
@juandopazo
juandopazo / gist:1031607
Created June 17, 2011 15:09
class sugar proposal
class Monster {
// Goes on the instance
own name;
// Goes on the instance and is private
// In the lexical context of a member of the class it can be accessed with {{Instance}}.health or {{Instance}}["health"]
private own health = 10;
constructor(name) {
this.name = name;
}
@juandopazo
juandopazo / gist:1032636
Created June 18, 2011 00:14
Simple inheritance implementation in JavaScript
window.Class = (function () {
function subclass(superclass, proto) {
proto = proto || {};
var constructor = proto.constructor;
if (!constructor) {
constructor = proto.constructor = function () {};
}
if (superclass) {
@juandopazo
juandopazo / main.js
Created June 18, 2011 16:41
Function.create for inheritance
var Person = Function.create(null, {
constructor: function (name) {
this.name = name;
},
say: function (message) {
return this.name + ' says: ' + message;
}
});
var Pirate = Person.extend({
@juandopazo
juandopazo / gist:1046912
Created June 25, 2011 21:21
Y.when with end()
// In this case async3() won't be executed instantly but rather after async1 and async2 are completed
Y.when(async1(), async2()).then(async3()).end(function () {
console.log('success');
}, function () {
console.log('failure');
});
@juandopazo
juandopazo / gist:1058478
Created July 1, 2011 12:50
Proto, a Selfish variant
////////// API //////////
// Code by Axel Rauschmayer
// Based on Selfish https://github.com/Gozala/selfish
// To be part of ECMAScript.next
if (!Object.getOwnPropertyDescriptors) {
Object.getOwnPropertyDescriptors = function (obj) {
var descriptors = {};
Object.getOwnPropertyNames(obj).forEach(function (prop) {
descriptors[prop] = Object.getOwnPropertyDescriptor(obj, prop);
@juandopazo
juandopazo / gist:1061739
Created July 2, 2011 22:41
One-tab-one-module pattern in YUI
// example module
YUI.add('foo', function (Y) {
// each of these modules must have a class named exactly like the module
function Foo() {}
// it's useful, in the case of tabs,
// for these classes to have a focus() method. Check below
Foo.prototype.focus = function () {};
Y.Foo = Foo;
@juandopazo
juandopazo / gist:1144127
Created August 13, 2011 18:27
Concatenar arrays anidados recursivamente
function aplaneitor2(arr){
return Array.isArray(arr) ? arr.map(aplaneitor2).join('+') : arr;
}
var test = ["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ];
console.log(aplaneitor2(test)) // -> hola+soy+juan+fernandez+y+no+tengo+dinero
@juandopazo
juandopazo / gist:1146157
Created August 15, 2011 12:47
Concatenar arrays anidados sin recursividad
function aplaneitor2_ES3(arr) {
arr = arr.concat(); //devolver un nuevo array, no modificar el mismo
var i = 0;
while (i < arr.length) {
if (Object.prototype.toString.call(arr[i]) == '[object Array]') {
Array.prototype.splice.apply(arr, [i, 1].concat(arr[i]));
} else {
i++;
}
}
@juandopazo
juandopazo / gist:1160119
Created August 21, 2011 04:28
Browser APIs and their weird dissimilitudes
// WEB WORKERS
var worker = new Worker('task.js');
worker.onmessage = function (e) {
};
// where's addEventListener? A Worker shoud be an EventEmitter
worker.addEventListener('message', function (e) {
});
// And you know, on() as an alias of addEventListener makes a lot of sense too