Skip to content

Instantly share code, notes, and snippets.

@incompl
incompl / ctor_name_inheritance.js
Created October 17, 2011 14:34
.name property, constructors, inheritance
// A weird bug when you inherit from a constructor. You shouldn't
// inherit from a constructor but when I accidentally did I noticed
// this weirdness.
function Foo() {}
// this should be Object.prototype, or {}, or omitted completely,
// but certainly not the Object constructor. Still, what follows
// isn't exactly what you'd expect...
Foo.prototype = Object;
@incompl
incompl / __proto__ dne constructor.prototype.js
Created October 20, 2011 02:46
Shows that .__proto__ is not the same as .constructor.prototype
function Parent() {}
var parent = new Parent();
function Child() {}
Child.prototype = parent;
var child = new Child();
// child inherits directly from parent. this makes sense.
console.log(child.__proto__ === parent); // true
console.log(child.__proto__ === Parent.prototype); // false
@incompl
incompl / box2dweb-example.js
Created May 11, 2012 14:44
Example of box2dweb with C++ heritage noted in comments
// Code from http://lib.ivank.net/?p=demos&d=box2D
// These look like imports, but in JavaScript they are just busy-work.
var
b2Vec2 = Box2D.Common.Math.b2Vec2,
b2BodyDef = Box2D.Dynamics.b2BodyDef,
b2Body = Box2D.Dynamics.b2Body,
b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
b2World = Box2D.Dynamics.b2World,
b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
@incompl
incompl / gist:2925304
Created June 13, 2012 17:08 — forked from buhrmi/gist:1344659
Sublime Text 2 Git Annotation Colors
<dict>
<key>name</key>
<string>Git Modified Line</string>
<key>scope</key>
<string>git.changes.x</string>
<key>settings</key>
<dict>
<key>background</key>
<string>#212340</string>
</dict>
<head>
...
<!-- source files for development -->
<!-- <script src="src/setup.js"></script> -->
<!-- <script src="src/player.js"></script> -->
<!-- <script src="src/enemy.js"></script> -->
<!-- <script src="src/bullet.js"></script> -->
<!-- <script src="src/explosion.js"></script> -->
<!-- concatenated / minified source for production -->
var state = [{
id: 0,
transform: {
position: {
x: 15.290663048624992,
y: 2.0000000004989023,
z: -24.90756910131313
},
rotation: {
x: 0.32514392007855847,
var char = new Int8Array( 1 );
// uchar now shares the same ArrayBuffer as char.
var uchar = new Uint8Array( char.buffer );
char.buffer === uchar.buffer; // true
char[0] = -1;
uchar[0] === 255; // true, same data interpreted differently
var arr = new Float64Array([15.290663048624992]);
// Compact the state.
var slimmerState = new Float64Array([
0,
15.290663048624992,
2.0000000004989023,
-24.90756910131313,
0.32514392007855847,
-0.8798439564294107,
0.32514392007855847,
0.12015604357058937,
var encodeUint8 = (function() {
var arr = new Uint8Array( 1 );
return function( number ) {
// If we assume that the number passed in
// valid, we can just use it directly.
// return String.fromCharCode( number );
arr[0] = number;
return String.fromCharCode( arr[0] );
};
}());
// Shared code
var MsgType = {
"game start": String.fromCharCode(0),
"update state": String.fromCharCode(1),
"private message": String.fromCharCode(2)
};
var MsgTypeLookup = [
"game start",
"update state",