Skip to content

Instantly share code, notes, and snippets.

/*function roundNumber(number, decimalPlaces) {
var decimals = number.toString().split('.')[1];
if (!decimalPlaces) return number;
if (!decimals || decimals.length < decimalPlaces) return parseFloat(number);
var originalNumDecimalPlaces = number.toString().split('.')[1].length,
newNumber = number * Math.pow(10, originalNumDecimalPlaces);
for (var i = originalNumDecimalPlaces; i > decimalPlaces; i--) {
newNumber = Math.round(newNumber / 10);
@wlmeurer
wlmeurer / better.js
Created June 17, 2011 17:26
Encapsulated Class Creation in JavaScript
var Person = (function(){
// all of the Person class definition is encapsulated in this closure
// private, class variable
var store = [];
// constructor
var PersonClass = function(name){
this.name = name;
store.push(tfhis);
@wlmeurer
wlmeurer / gist:671630
Created November 10, 2010 22:08
Weird Chrome console behavior
var arr = [];
for(var i=0;i<3;i++){
arr.push(i);
if(i==2)arr='blah';
console.log('foo',arr);
}
/*
(2) foo [0,1,2]
foo blah