Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// Types
var one = 1;
var two = 2;
three;
window.three;
window.one = 1;
one = 1;
var one = [];
var one = {one: 1};
one.two = 1;
[] + {};
{} + [];
"1" + null;
undefined = true;
"1" == 1;
"1" === 1;
Math.sqrt(-1);
Math.sqrt(-1) === Math.sqrt(-1);
// functions
var fn = function(){ console.log(this.name); };
window.name = 'jeff!';
fn();
// semicolons
var newFn = function(){
return
'Why hi there!';
};
newFn();
// this
var a = {name: 'a'};
var b = {name: 'b'};
fn.call(a);
fn.call(b);
// arguments
(function iffe(){
console.log(arguments);
console.log(arguments.callee);
console.log(arguments.caller); // deprecated
console.log(arguments.length);
console.log(arguments.__proto__); // not array [];
})(one, two);
['one', 'two', 'three'].slice();
// controlling this and partial application
var bind = function(fn, self){
return function(){
var args = [].slice.call(arguments);
return fn.apply(self, args)
};
};
var bound = bind(a, fn);
bound.call(b);
// prototypes
a.hasOwnProperty('name');
a.__proto__;
var MyClass = function(){
this.newName = 'Oh NO!';
console.log(this);
};
MyClass();
console.log(newName);
var c = new MyClass();
MyClass.prototype.hello = function(){
console.log('HELLO NICAR!');
};
c.hello();
MyClass.prototype.hello = function(){
console.log('BYE!');
};
c.hello();
c.__proto__;
// inheritance in javascript
super // <- oh no
var inherits = function(parent, child) {
var ctor = function(){};
ctor.prototype = parent.prototype;
child._super = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
};
var Other = function(){
MyClass.call(this);
};
inherits(MyClass, Other);
var o = new Other();
o.hello();
Other.prototype.hasOwnProperty('hello');
Other.prototype.hello;
Other.prototype.__proto__.hasOwnProperty('hello');
Other.prototype.__proto__;
Other.prototype.__proto__.constructor;
// Ok, now for the main event, writing evented code!
var extend = function(obj){
var args = [].slice.call(arguments);
for(var i = 0; i < args.length; i++)
for(var j in args[i])
obj[j] = args[i][j];
return obj;
};
extend({}, {name: 'hello!'}, {tag: '#NICAR'});
var events = {
on : function(ev, fn){
this._callbacks = this._callbacks || {};
this._callbacks[ev] = this._callbacks[ev] || [];
this._callbacks[ev].push(fn);
}
};
events.off = function(ev, fn){
if(!this._callbacks[ev]) return;
var cbs = this._callbacks[ev], args = [].slice.call(arguments);
for(var i = 0; i < cbs.length; i++)
if(cb === fn) cbs.slice(1, i);
};
events.trigger = function(ev) {
if(!this._callbacks[ev]) return;
var cbs = this._callbacks[ev], args = [].slice.call(arguments);
for(var i = 0; i < cbs.length; i++)
cbs[i].apply(this, args);
};
var Publisher = function(){
this.hello = 'yeah';
this.on('hello', function() { console.log(this.hello); });
};
extend(Publisher.prototype, events);
var a = new Publisher();
var Listener = function(){
a.on('hello', function(){ console.log('heyo!'); });
};
var l = new Listener();
a._callbacks
a.trigger('hello');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment