Skip to content

Instantly share code, notes, and snippets.

@Armen138
Armen138 / Structured_Javascript.js
Created March 5, 2012 19:42
Coding rules for Structured Javascript
/*
Structured Javascript
* using quotes
In C/C++ tradition, use double quotes ("") for strings, and single quotes ('') for characters.
If a string contains other strings(such as xml elements), use double quotes for the "outside" quotes,
singe quotes for the "inside" quotes.
* class declaration
Always capitalize class names.
@Armen138
Armen138 / jsclasses.js
Created March 6, 2012 01:56
Structured Javascript Example
var AClass = function () {
"use strict";
//Private variables and functions
var self = this,
priv1 = 66,
priv2 = 2,
privMethod = function () {
return priv2;
},
privMethod2 = function () {
@Armen138
Armen138 / global.js
Created March 13, 2012 02:09
Javascript Global Object
var single = {
importantString: "something important",
importantObject: new ImportantObject(),
importantNumber: 138
};
@Armen138
Armen138 / globalinstance.js
Created March 13, 2012 02:11
JS Global Instance
var Single = new function () {
var somethingPrivate = 138;
this.importantString = "something important";
this.importantObject = new ImportantObject();
this.importantMethod = function () {
returh somethingPrivate + 55;
};
};
@Armen138
Armen138 / jsevents.js
Created March 22, 2012 03:33
Javascript Events
var MyEvents = function() {
"use strict";
var self = this,
eventList = {};
this.addEventListener = function(eventName, callback) {
if(!eventList[eventName]) {
eventList[eventName] = [];
}
eventList[eventName].push(callback);
@Armen138
Armen138 / qdip.js
Created July 17, 2012 02:18
Quick+Dirty Image Preloader
/* quick + dirty image preloader */
qdip = {
total: 0,
loaded: 0,
images: {},
load: function(files) {
function loaded(file) {
qdip.loaded++;
qdip.trigger("progress", file);
if(qdip.loaded === qdip.total) {
@Armen138
Armen138 / bindshim.js
Created July 17, 2012 11:54
Function.prototype.bind for javascriptcore
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
@Armen138
Armen138 / simplex.js
Created July 24, 2012 18:43
Simplex Noise
/*
* A speed-improved simplex noise algorithm for 2D, 3D and 4D in Java.
*
* Based on example code by Stefan Gustavson (stegu@itn.liu.se).
* Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
* Better rank ordering method by Stefan Gustavson in 2012.
*
* This could be speeded up even further, but it's useful as it is.
*
* Version 2012-03-09
@Armen138
Armen138 / spiral.js
Created August 17, 2012 20:36
Spiral
var x = 0,
y = 0,
dx = 0,
dy = -1;
for(var i = 0; i < 10; i++) {
if( (x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1-y))){
t = dx;
dx = -dy;
@Armen138
Armen138 / nodearray.cpp
Created November 27, 2012 22:05
nodejs module test in c++
#include <v8.h>
#include <node.h>
#include <iostream>
#include <string>
using namespace node;
using namespace v8;
static Handle<Value> foo(const Arguments& args)
{