Skip to content

Instantly share code, notes, and snippets.

View Cycymomo's full-sized avatar

Cyril Moreau Cycymomo

View GitHub Profile
@jrburke
jrburke / gist:1262861
Created October 4, 2011 21:26
Universal (AMD/Node/plain browser) module
/**
* First, better, "set exports/return" option
*/
(function (define) {
//The 'id' is optional, but recommended if this is
//a popular web library that is used mostly in
//non-AMD/Node environments. However, if want
//to make an anonymous module, remove the 'id'
//below, and remove the id use in the define shim.
define('id', function (require) {
@Cycymomo
Cycymomo / explainDuckTyping.md
Last active December 20, 2015 02:59
héritage délégation javascript

En JavaScript, on ne teste pas qu'un objet appartient à telle classe ou implémente telle interface. Cela n'a pas de sens car un objet JavaScript n'entre dans aucun moule préalablement défini

En gros, en JavaScript, tu ne crées pas un objet à partir d'un moule (classe) qui doit implémenter telle interface, mais tu crées un objet A à partir d'un autre objet B. B devient alors le prototype de A. Le prototype "racine" étant [CODEINLINE]Object[/CODEINLINE]. Le nouvel objet B ainsi créé à partir de A aura ses propres méthodes/attributs et aura également accès à celles de A, puis ensuite à celles du prototype de A, etc. On parle de chaîne de prototype. Si la propriété/méthode n'est pas dans B, on check dans son prototype A, puis dans le prototype du prototype A, etc, jusqu'à remonter la pile jusqu'à [CODEINLINE]Object[/CODEINLINE].

Un "équivalent" de l'interface serait plutôt de tester la présence d'attributs ou de méthodes.

En effet, JavaScript répond au style "[URL="http://fr.wikipedia.org/wiki/Duck_typing"]duc

@Cycymomo
Cycymomo / bindEvent.js
Last active December 20, 2015 12:19
bindEvent.js
var bindEvent = function( element, type, eventHandler ) {
if ( element.addEventListener ) {
element.addEventListener( type, eventHandler, false );
} else if ( element.attachEvent ) {
element.attachEvent( "on" + type, eventHandler );
}
};
bindEvent(document.getElementById('mon_element'), 'change', function () {
alert('je change !');
@addyosmani
addyosmani / examples.md
Last active February 23, 2016 18:22
Object.observe() examples from my talk

What are we trying to observe? Raw object data.

// Objects
var obj = { id: 2 };
obj.id = 3; // obj == { id: 3 }
 
// Arrays
var arr = ['foo', 'bar'];
arr.splice(1, 1, 'baz'); // arr == ['foo', 'baz'];
@Cycymomo
Cycymomo / explainHeritage1.md
Last active December 28, 2015 07:59
explainHeritage

Comment faire de l'héritage en JavaScript ?

ou la POO classique, c'est pas automatique !

tl;dr

Cet article présente les différentes manières de faire de l'héritage en JavaScript. Il répondra aussi à ces questions :

  • Y a t il des classes en JavaScript comme dans les autres langages ?
  • Quelles sont les différentes façons de faire de l'héritage en JavaScript ?
  • Quelle est la bonne pratique ?

De la POO en JavaScript, sans classe ?

echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://npmjs.org/install.sh | sh
@xem
xem / README.md
Last active May 15, 2016 17:31 — forked from 140bytes/LICENSE.txt
Challenge: encode / decode 400 (or more) ASCII characters in a single tweet!

Hi,

A tweet can contain 140 UTF-16 characters.

An UTF-16 character can be composed of 2 16-bits surrogates.

A UTF-16 surrogate can be used to store 10 bits.

An ASCII character is 7 bits long.

/**
* Create an array with `len` elements.
*
* @param [initFunc] Optional: a function that returns
* the elements with which to fill the array.
* If the function is omitted, all elements are `undefined`.
* `initFunc` receives a single parameter: the index of an element.
*/
function initArray(len, initFunc) {
if (typeof initFunc !== 'function') {
@rauschma
rauschma / newOperator.js
Created December 11, 2013 23:32
This is roughly how one would implement the `new` operator in JavaScript.
function newOperator(Constr, args) {
var inst = Object.create(Constr.prototype);
var result = Constr.apply(inst, args);
if (typeof result === 'object' && result !== null) {
return result;
}
return inst;
}
/**
* POST to create a new user.
*/
exports.create = function *(){
var body = yield parse(this);
// password
var pass = body.password;
assert(pass, 400, 'password is required');