Homework Schedule
Posted by
Toby
10 days ago
(0 comments)










var self = this
var Man = Class(Object)({
__init__: function(self, name){
self.name = name
},
greeting: function(self, other){
setTimeout(function(){
self.say("Hello, " + other.name + ", my name is " + self.name)
}, 100)
},
say: function(self, msg){
sys.puts(msg)
}
})
var dan = new Man('Dan')
var john = new Man('John')
dan.greeting(john)
john.say('How do?')
How do? Hello, John, my name is Dan
function toArray(args){
var ret = []
for (var i = 0; i < args.length; i++)
ret.push(args[i])
return ret
}
function Class(parent){
function bind(func, obj){
var ret = function(){
var args = toArray(arguments)
args.splice(0, 0, obj)
return func.apply(undefined, args)
}
return ret
}
return function(attrs){
return function(){
for (var name in attrs){
var value = attrs[name]
if (typeof(value) == 'function'){
this[name] = bind(value, this)
this[name].name = name
}else{
this[name] = value
}
}
this.__init__.apply(this, arguments)
}
}
}
var JSONP = {}
JSONP.get = function(url, callback){
var scriptTag = document.createElement('script')
var callbackName = '_' + new Date().getTime()
JSONP[callbackName] = function(){
callback.apply(null, arguments)
delete JSONP[callbackName]
document.head.removeChild(scriptTag)
}
if (url.indexOf('?') != -1)
url += '&callback=JSONP.' + callbackName
else
url += '?callback=JSONP.' + callbackName
scriptTag.src = url
document.head.appendChild(scriptTag)
}
JSONP.get('http://feeds.delicious.com/feeds/json/tags/your_username', function(data){// Woohoo! You got data!
})
function getText(elm, excludeTags){
if (elm.nodeType == 3) return elm.nodeValue;
if (excludeTags && elm.tagName && excludeTags.indexOf(elm.tagName.toLowerCase()) != -1) return '';
var ret = '';
for (var i = 0; i < elm.childNodes.length; i++){
ret += getText(elm.childNodes[i], excludeTags);
}
return ret;
}
function keys(obj){
var ret = [];
for (var key in obj) ret.push(key);
return ret;
}
var text = getText(document.body, ['script']);
var commonWords = ['a', 'the', 'and', 'of', 'is', 'in', 'this', 'it', 'to'];
var words = text.replace(/[^a-zA-Z]/g, ' ').split(' ').filter(function(p){return p != ''});
words = words.filter(function(word){
return commonWords.indexOf(word) == -1;
});
var freq = {};
words.forEach(function(word){
freq[word] = (freq[word] || 0) + 1;
});
var markup = keys(freq).map(function(word){
return '<span style="font-size: ' + (freq[word] / 4) + 'em">' + word + '</span>';
}).join(' ');
document.body.innerHTML = markup;
function sqrt(a, b){return a * 2 + b * 2;
}
function sqrt(a, b, callback){ multiply(a, 2, function(a2){ multiply(b, 2, function(b2){add(a2, b2, callback);
});
});
}
function sqrt(a, b){ var a2 = a * 2;
var b2 = b * 2;
return a2 * b2;
}
function sqrt(a, b, callback){ multiply(a, 2, function(a2){ /* code */
multiply(b, 2, function(b2){ /* code */
});
/* more code here */
});
}
function asCPS(f){
var _f = function(){
var callback = arguments[arguments.length - 1];
var args = Array
.prototype.slice
.call(arguments, 0, arguments.length - 1)
var retVal = f.apply(this, args)
if (callback) callback(retVal)
}
_f.name = f.name
return _f
}
function add(one, other){
return one + other;
}
add = asCPS(add)
add(1, 2, function(sum){
console.log('sum: ' + sum)
})