Skip to content

Instantly share code, notes, and snippets.

@codebycliff
Created January 15, 2013 05:05
Show Gist options
  • Save codebycliff/4536270 to your computer and use it in GitHub Desktop.
Save codebycliff/4536270 to your computer and use it in GitHub Desktop.
JS Tidbits
alert(this)
window.alert(window)
location.href = "http://google.com";
var o = new Object();
o.name = "bob";
o.age = 37;
console.log(o.name);
delete o.age;
// Object literal notation
var bob = {
name: "bob",
age: 37,
talk: function() {
alert(this.name);
},
yearBorn: function() {
var now = new Date();
return now.getYear() - this.age;
}
}
var Person = function(name, age) {
this.name = name;
this.age = age;
this.talk = function() { alert(this.name); }
}
var al = new Person("al", 48);
al['talk']()
var callback = function(x, y) {
alert(x+y);
}
callback(2,3);
Array.prototype.forEach = function(cb) {
for(var i = 0; i < this.length; i ++) {
cb(this[i]);
}
}
[1,2,3].forEach(window.alert);
String.prototype.parseInt = function() {
return parseInt(this);
}
String.prototype.alert = function(str) {
alert(str);
}
var gallery = document.createElement("div");
gallery.id = gallery;
document.getElementsByName("body")[0].appendChild(gallery);
var img = new Image();
img.src = "/image.jpg";
img.alt ="...";
var gallery = document.getElementById("gallery");
gallery.appendChild(img);
var script = document.createElement("script");
script.src="file.js";
script.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(script);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment