Skip to content

Instantly share code, notes, and snippets.

@thebigbad
Created December 28, 2009 20:06
Show Gist options
  • Save thebigbad/264892 to your computer and use it in GitHub Desktop.
Save thebigbad/264892 to your computer and use it in GitHub Desktop.
// Javascript Arrays already have:
// Array.indexOf
// Array.lastIndexOf
// Array.every
// Array.filter
// Array.forEach
// Array.map
// Array.some
Array.prototype.remove = function (from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
Array.prototype.max = function () {
return Math.max.apply(Math, this);
};
Array.prototype.min = function () {
return Math.min.apply(Math, this);
};
// Keywords and Reserved words
// All Java keywords are Javascript reserved words. Don't use reserved words in your code!
// Comments
// Beware that /* */ can be used in a regular expression, and that has the potential to
// fuck you up.
// Numbers
// JavaScript has 64-bit floats, no integers.
// Booleans, Equivalence, and "Falsiness"
// `==` is not transitive and conditional statements may behave strangely.
// The following keywords and primitives are "falsy":
// false, null, undefined, '', NaN, 0
NaN == false; // Evaluates to false
if (NaN) print("false"); // Prints nothing.
// Scope
// Blocks do not create a new scope; only functions do.
{
var foo = 0; // Global variable
}
var f = function () {
var foo = 1; // Local variable
};
f();
print(foo); // Prints 0
// Objects
obj.foo; // Property foo is resolved using the prototype chain
delete obj.foo; // Property foo is removed from obj
var x = new Foo(); // Makes x an instance of Foo
var x = Foo(); // Makes the global object an instance of Foo
// Object methods
Object.prototype.isPrototypeOf([]); // true
Array.prototype.isPrototypeOf([]); // true
[1, 2, 3].propertyIsEnumerable("1"); // true
[1, 2, 3].propertyIsEnumerable("forEach"); // false
// Functions and Arguments
var g = function () {
var self = this; // Implicit parameter to function
var args = arguments; // Implicit parameter to function
// However, arguments is not an array
};
// Method invocation pattern
// What is `this`?
var x = {
foo: 0,
bar: function () {
this.foo = 1; // Refers to x.foo
}
};
// Function invocation pattern
// What is `this`?
var f = {
this.foo = 1; // Refers to global object
};
// Apply invocation pattern
// What is `this`?
var f = {
this.foo = 1; // Refers to global object here
};
f.apply(x, []); // `this` refers to x here
// Prototypes
// If you say `foo.bar`, and `foo` does not have property `bar`, JavaScript will check
// if `foo.constructor.prototype` has `bar`. If not, then it will check up the prototype chain.
// Remember to set the prototype on the object's constructor, which will let all instances of
// that object check its prototype chain to resolve fields. Do not set prototype on an object
// itself.
Foo.prototype = new Bar();
var foo = new Foo();
foo.constructor.prototype === Foo.prototype // Prints true
// Hackety hash function in Jabba the script
var hash = function (value) {
return (typeof value) + ' ' +
(value instanceof Object ?
(value.hashCode || (value.hashCode = ++arguments.callee.current)) :
value.toString()
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment