Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

曾浩 think2011

🏠
Working from home
View GitHub Profile
@think2011
think2011 / javascript.array.unique
Last active Aug 29, 2015
javascript.array.unique
View javascript.array.unique
/**
* 数组去重
* @param key
* @returns arr
* @example var newArr = arr.unique('nickname');
*/
Array.prototype.unique = function (key) {
var key = key || null, temp;
var arr = this,
hash = {};
@think2011
think2011 / ng-bind-html
Created May 8, 2014
angularjs,这是一个 scope 中带有 html 代码的处理方式。
View ng-bind-html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div test ng-bind-html="title"></div>
<script>
View gist:be2f951bae284086f309
# 提取复姓
#
# @param [String] name
# @example name = getHyphenated '曾浩'
#
getHyphenated = (name) ->
if !name then return
hyphenated = ["欧阳", "太史", "端木", "上官", "司马", "东方", "独孤", "南宫", "万俟", "闻人", "夏侯", "诸葛", "尉迟", "公羊", "赫连", "澹台", "皇甫", "宗政", "濮阳", "公冶", "太叔", "申屠", "公孙", "慕容", "仲孙", "钟离", "长孙", "宇文", "城池", "司徒", "鲜于", "司空", "汝嫣", "闾丘", "子车", "亓官", "司寇", "巫马", "公西", "颛孙", "壤驷", "公良", "漆雕", "乐正", "宰父", "谷梁", "拓跋", "夹谷", "轩辕", "令狐", "段干", "百里", "呼延", "东郭", "南门", "羊舌", "微生", "公户", "公玉", "公仪", "梁丘", "公仲", "公上", "公门", "公山", "公坚", "左丘", "公伯", "西门", "公祖", "第五", "公乘", "贯丘", "公皙", "南荣", "东里", "东宫", "仲长", "子书", "子桑", "即墨", "达奚", "褚师"]
@think2011
think2011 / currentJSDir.coffee
Created Oct 27, 2014
获取当前js文件的路径,需要写在文件顶部。
View currentJSDir.coffee
currentJSDir = do ->
js = document.scripts[document.scripts.length-1]
return js.src.substr 0, js.src.lastIndexOf '/'
@think2011
think2011 / angular-trustAsHtml
Created Nov 4, 2014
angular-显示html代码
View angular-trustAsHtml
// js
app.controller('appCtrl', function ($sce) {
$scope.trustAsHtml = function (str) {
return $sce.trustAsHtml(str);
};
});
// html
<div ng-bind-html="trustAsHtml(item.content)"></div>
@think2011
think2011 / drag.js
Created Dec 17, 2014
简单的拖动功能
View drag.js
var box = document.querySelector('#box');
box.onmousedown = function (e) {
box.style.position = 'absolute';
var diffX = e.clientX - box.offsetLeft;
var diffY = e.clientY - box.offsetTop;
document.onmousemove = function (e) {
box.style.left = e.clientX - diffX + 'px';
box.style.top = e.clientY - diffY + 'px';
}
@think2011
think2011 / gist:9f12524edc1bda68845e
Last active Aug 29, 2015
new constructor的过程
View gist:9f12524edc1bda68845e
1. 创建一个新的对象,这个对象的类型是object。
2. 该对象的prototype设置为构造器的prototype属性,即this.prototype = Foo.prototype (伪代码)
3. 执行构造器函数。
4. 如果构造器函数有返回值,则以该对象作为返回值。若没有return或return了基本类型,则将上述的新对象作为返回值。
@think2011
think2011 / upload.js
Last active Aug 29, 2015
node.js 上传图片实例
View upload.js
var formidable = require('formidable'),
http = require('http'),
util = require('util'),
fs = require('fs');
http.createServer(function(req, res) {
// 移除跨域限制
res.setHeader('Access-Control-Allow-Origin', '*');
@think2011
think2011 / bind.js
Created Jan 14, 2015
允许二次bind的bind函数
View bind.js
Function.prototype.bind = function (fn) {
var ret = function () {
return ret._fn.apply(fn);
}
ret.fn = this._fn || this;
return ret;
}
View 九九乘法表.js
var i = 1, j = i, ret = '';
for(; i <= 9;) {
if(j > i) {
ret += '\n';
j = 1;
i++;
continue;
}