Check if an object is not empty

by Mohan 2012-09-20 21:56:20


Check if an object is not empty

// JavaScript 1.5
function isNotEmpty(obj) {
for (var tmp in obj)
return true
}

// JavaScript 1.8
function isNotEmpty(obj) obj.__count__;
Transform the arguments object into an array

function foo() {
Array.slice(arguments); // is ['aa',11]
}
foo('aa', 11);
JavaScript Minifier / comment remover

var script = new Script('var a; /* this is a variable */ var b; ' +
'// another variable');
Print( script.toString() );
// prints:
// var a;
// var b;
Singleton pattern

function MySingletonClass() {
if ( arguments.callee._singletonInstance )
return arguments.callee._singletonInstance;
arguments.callee._singletonInstance = this;
this.Foo = function() {
// ...
}
}

var a = new MySingletonClass();
var b = MySingletonClass();
Print( a === b ); // prints: true
Factory method pattern

Complex = new function() {
function Complex(a, b) {
// ...
}
this.fromCartesian = function(real, mag) {
return new Complex(real, imag);
}
this.fromPolar = function(rho, theta) {
return new Complex(rho * Math.cos(theta), rho * Math.sin(theta));
}}
var c = Complex.fromPolar(1, Math.pi); // Same as fromCartesian(-1, 0);
690
like
0
dislike
0
mail
flag

You must LOGIN to add comments