Skip to content

Instantly share code, notes, and snippets.

View winsonwq's full-sized avatar
🎯
Focusing

Wang Qiu winsonwq

🎯
Focusing
  • 可好玩乐
  • Chengdu China
View GitHub Profile
@winsonwq
winsonwq / bubbleSortOfMrAsyncInterpreter.js
Created January 3, 2012 08:21
BubbleSort of Mr.Async.Interpreter
// 算法本身
for(var i = 0 ; i < arr.length - 1 ; i++){
for(var ii = i + 1; ii < arr.length ; ii++){
if(arr[i] > arr[ii])
normalSwap(arr, i, ii);
}
}
// 利用了Mr.Async.Interpreter的方式
eval(Mr.Async.recode(function(){
@winsonwq
winsonwq / MrDeferredInAwait.js
Created January 3, 2012 08:24
How to use $await in Mr.Async
// 一个实现了Mr.Deferred的通用异步方法
// 等待一秒钟,计算一个0到1的随机数
function asyncRandom(callback){
var de = Mr.Deferred();
setTimeout(function(){
var random = Math.random();
if(typeof callback == 'function')
callback(random);
de.resolve(random);
}, 1000);
@winsonwq
winsonwq / simpleAwait.js
Created January 3, 2012 08:26
How to use Mr.Async.recode method
eval(Mr.Async.recode(function(){
var i = $await(delay()); // 等待
console.log(i);
}));
@winsonwq
winsonwq / JSInheritance.js
Created February 13, 2012 16:12
JavaScript Inheritance (not 100% perfect)
/*
why is it not 100% perfect?
- because if you wanna override the parent's method to get the private variable and use
my way to extend, you can not get the right value. in this code, a example can be found
to demonstrate the right way to use extend method, in this example, is you change the 'title'
variable like this way 'var title' not 'this.title', finally you just get the undefined value
after invoking the getTitle on Child class instance.
- any question, contact me through http://sheldonw.sinaapp.com
*/
@winsonwq
winsonwq / uploadImages.js
Created March 4, 2012 16:32
upload images one by one
require(['./lib/Mr.Async', './lib/Recoder/main'], function(Mr, interpreter){
function asyncUpload(image){
var dfd = Mr.Deferred();
ajaxPost(image, function(ret){
if(ret.status == 200){
dfd.resolve();
}
});
return dfd;
@winsonwq
winsonwq / renderView.js
Created March 4, 2012 16:54
pseudocode of renderView
require(['./lib/Mr.Async', './lib/Recoder/main'], function(Mr, interpreter){
function getListData(url, callback){
$.get(url, callback, 'json');
}
function getSideBarData(url, data, callback){
$.get(url, data, callback, 'json');
}
@winsonwq
winsonwq / bubbleAndCapture.js
Created April 20, 2012 16:29
bubble and capture
var outer = document.getElementById('outer');
var inner = document.getElementById('inner');
outer.addEventListener('click', function(evt){
console.log(evt.target);
alert(0);
}, false);
inner.addEventListener('click', function(evt){
console.log(evt.currentTarget);
@winsonwq
winsonwq / once.js
Created April 20, 2012 16:34
once implementation
function once(elem, eventType, handler){
elem.addEventListener(eventType, function(evt){
handler.call(elem, evt);
elem.removeEventListener(eventType, arguments.callee/*, false */);
}, false)
}
once(document.getElementById('onceButton'), 'click', function(evt){
alert('just show once!');
});
@winsonwq
winsonwq / stop(Immediate)Propagation.js
Created April 20, 2012 16:36
stop(Immediate)Propagation
var outer2 = document.getElementById('outer2');
var inner2 = document.getElementById('inner2');
var stopChecked = document.getElementById('stopChecked');
outer2.addEventListener('click', function(evt) {
alert('outer click 1.');
}, false);
inner2.addEventListener('click', function(evt) {
alert('inner click 1.');
@winsonwq
winsonwq / git-note
Created May 26, 2012 02:27
Git note
Wikipedia explanation: http://zh.wikipedia.org/wiki/Git#.E5.91.BD.E5.90.8D.E6.9D.A5.E6.BA.90