Skip to content

Instantly share code, notes, and snippets.

View bennyjo's full-sized avatar

Benny Johansson bennyjo

View GitHub Profile
@bennyjo
bennyjo / New line
Created November 19, 2014 12:46
New line vs no new line
function rqGrid($window, rqUrl, columns, module, settings) {
function rqGridLink(scope, $element) {
var itemIdColumnName = columns.getItemIdDataField();
var jqx = new JqxGridInterface($element);
if (settings) {
jqx.call('loadState', settings);
}
@bennyjo
bennyjo / gist:7d147bdd69a59ed78fe0
Created November 4, 2014 10:16
Service vs Factory
app.service('myService', function() {
// service is just a constructor function
// that will be called with 'new'
this.sayHello = function(name) {
return "Hi " + name + "!";
};
});
@bennyjo
bennyjo / gist:845253
Created February 26, 2011 14:32
[JS] Who called?
function returnMe() {
return arguments.callee;
}
console.log(returnMe()); //=> returnMe()
@bennyjo
bennyjo / gist:845244
Created February 26, 2011 14:30
[JS] Steal method from other objects
function takesVariableArgsWithOptionalHash() {
var options = {};
var otherArgs = arguments;
// steal slice from Array.prototype
var slice = Array.prototype.slice;
if (typeof arguments[arguments.length-1] == 'object') {
options = arguments[arguments.length-1];
otherArgs = slice.call(arguments, 1);
@bennyjo
bennyjo / gist:845235
Created February 26, 2011 14:22
[JS] Variable number of function arguments
// add up all the arguments sent to the function
// no matter how many there are
function sum() {
var total = 0;
for (var i = 0, l = arguments.length; i < l; i++)
total += arguments[i];
return total;
@bennyjo
bennyjo / gist:845221
Created February 26, 2011 14:05
[JS] Function called as constructor or normal function
function Element() {
if (this == window || 'Element' in this) {
// the function is being called normally
}
else {
// the function is being called via new
}
}
@bennyjo
bennyjo / gist:845111
Created February 26, 2011 11:02
[JS] Dynamic property names
var myObj = {};
for (var i = 0; i < 5; i++) {
myObj['prop_' + i] = i;
}
myObj.prop_2 //=> 2