Skip to content

Instantly share code, notes, and snippets.

View linx4200's full-sized avatar

liuxinran linx4200

  • Shopee
  • Shenzhen, China
View GitHub Profile
@linx4200
linx4200 / sort-by-multiple-keys.rb
Last active August 7, 2016 15:50
根据多个键值来对数组排序
def sort_by_multiple_keys(arr)
# <=> operator to compare each element of the collection.
# This operator takes two arguments
# returns
# -1 if the first argument is less than the second argument
# 1 if the second argument is less than the first argument
# 0 if they're equivalent.
# 1 <=> 2 # => -1
@linx4200
linx4200 / hijack-zhihu-copy.js
Last active August 17, 2016 02:10
你懂的。
$($('.zm-item-rich-text')).off().on('copy', function(){console.log('我hijack了你的copy!')});
@linx4200
linx4200 / change-the-author-in-git.bash
Created April 27, 2017 14:18
change-the-author-in-git
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
@linx4200
linx4200 / patterns-inheritance-classical.js
Created March 17, 2018 07:53
【继承】类式继承
function SuperClass(params) {
this.superValue = true;
}
SuperClass.prototype.getSuperValue = function() {
return this.superValue;
}
function SubClass () {
this.subValue = false;
@linx4200
linx4200 / patterns-inheritance-constructor.js
Created March 17, 2018 08:00
【继承】构造函数继承
function SuperClass(id) {
this.id = id;
this.books = ['a' ,'b', 'c'];
}
SuperClass.prototype.showBooks = function () {
console.log(this.books);
}
function SubClass (id) {
@linx4200
linx4200 / patterns-inheritance-prototypal.js
Created March 17, 2018 12:32
【继承】原型式继承
function inheritObject(o) {
// 声明一个过渡函数对象
function F () {};
// 过渡对象的原型继承父对象
F.prototype = o;
// 返回过渡对象的一个实例
return new F();
}
@linx4200
linx4200 / patterns-factory-simple.js
Created March 17, 2018 12:51
【Factory】最简单的工厂模式
var BasketBall = function () { /* ... */};
var FootBall = function () { /* ... */};
var Tennis = function () { /* ... */};
// 运动工厂
var sportsFactory = function (name) {
switch(name) {
case 'NBA':
return new BasketBall();
case 'WorldCup':
@linx4200
linx4200 / patterns-factory-safe-factory-method.js
Created March 17, 2018 14:13
【Factory】安全的工厂方法
// 安全模式创建的工厂类
var Factory = function(type, content) {
if (this instanceof Factory) {
return new this[type](content);
} else {
return new Factory(type, content);
}
}
// 工厂原型中设置创建所有类型数据对象的基类
@linx4200
linx4200 / patterns-factory-singleton.js
Created March 17, 2018 15:05
【Factory】单例模式
var A = {
Util: {
util_method1: function() {},
util_method2: function() {},
// ...
},
Tool: {
tool_method1: function() {},
tool_method2: function() {},
// ...
@linx4200
linx4200 / patterns-factory-lazy-singleton.js
Created March 17, 2018 15:09
【Factory】惰性单例
var LazySingleton = (function() {
// 单例实例引用
var instance = null;
// 单例
function Singleton() {
return {
publicMethod: function() {},
publicProperty: '1'
}
}