Skip to content

Instantly share code, notes, and snippets.

@lyuehh
lyuehh / object_inheritance.js
Created March 12, 2012 13:33
javascript: Prototypal inheritance
Object.beget = (function(Function){
return function(Object){
Function.prototype = Object;
return new Function;
}
})(function(){});
/*
It's a killer! Pity how almost no one uses it.
It allows you to "beget" new instances of any object, extend them, while maintaining a (live) prototypical inheritance link to their other properties. Example:
*/
@lyuehh
lyuehh / fauxconsole_w.js
Created April 24, 2012 03:44
console.log for ie
/* Faux Console by Chris Heilmann http://wait-till-i.com */
var ie = function() {
var v = 4,
//原作者的此处代码是3,考虑了IE5的情况,我改为4。
div = document.createElement('div'),
i = div.getElementsByTagName('i');
do {
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->';
} while (i[0]);
return v > 5 ? v : false; //如果不是IE,之前返回undefined,现改为返回false。
@lyuehh
lyuehh / scroll_ie.js
Created May 25, 2012 07:22
scroll in ie
scroll: function(start, end) { // 是否在滚动的标志位
var _scroll = false; // 初始化获取滚动的高度
var _top = $(document).scrollTop(); // 监听滚动是否开始结束
var _scroll = setInterval(function() {
var __top = $(document).scrollTop();
if (!_scroll && _top != __top) { // 滚动开始
_scroll = true;
if (typeof start == 'function') start();
}
if (_scroll && _top == __top) {
@lyuehh
lyuehh / get_img.js
Created August 31, 2012 11:30
nodejs download img
var http = require('http');
var fs = require('fs');
http.get('http://25.media.tumblr.com/tumblr_m98u7dXTPQ1qz7hmlo1_500.jpg',function (res) {
console.log(res.statusCode);
console.log(JSON.stringify(res.headers));
var imgdata = "";
res.setEncoding('binary');
res.on('data',function (chunk) {
@lyuehh
lyuehh / extend_example.js
Created October 18, 2012 03:09
extend a view in Backbone
// demo: http://jsfiddle.net/ShUW2/2/
var P = Backbone.View.extend({
events: {
'click p': 'p',
'change #a': 'change',
'click #s': 'click'
},
p: function (e) {
alert('clicked the p');
@lyuehh
lyuehh / event.html
Created October 18, 2012 06:44
click a input in ie
<!--demo from : http://www.cnblogs.com/rubylouvre/archive/2012/10/18/2729388.html -->
<!DOCTYPE HTML>
<html>
<head>
<title>onbeforeactivate by 司徒正美</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
window.onload = function(){
@lyuehh
lyuehh / func.js
Created October 22, 2012 03:07
js func
//方法调用模式
var obj = {
val: 0,
inc: function(i) {
this.val += i;
}
};
console.log(obj.val);
obj.inc(2);
obj.inc(2);
@lyuehh
lyuehh / json.js
Created October 25, 2012 01:39
json
JSON 字面量
>>> ({"a":"\a"})
Object { a="a"}
>>> ({"a":"\\a"})
Object { a="\a"}
>>> ({"a":"\\\a"})
Object { a="\a"}
@lyuehh
lyuehh / bind.js
Created October 28, 2012 11:20
Function.prototype.bind
Function.prototype.bind_a = function() {
var method = this,
slice = Array.prototype.slice,
args = slice.apply(arguments),
obj = args.shift();
return function() {
var arg = slice.apply(arguments);
return method.apply(obj,args.concat(arg));
};
@lyuehh
lyuehh / stepEngine.js
Created October 29, 2012 15:34 — forked from bang590/stepEngine.js
stepEngine - simplify everyauth asynchronous resolution
var fs = require('fs'),
http = require('http');
var Promise = function(values) {
this._callbacks = [];
this._errbacks = [];
if (arguments.length > 0) {
this.fulfill.apply(this, values);
}
}