搭建你自己的Hexo博客

Hexo基于node.js,可用于生成静态博客,结合github和Mac,可以专注创作了。
深入学习见文末引用。

hexo安装

brew install node
sudo npm install hexo-cli -g  #全局安装

创建博客

Hexo使用命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hexo init <folder>  #执行init命令初始化hexo到你指定的目录
hexo new [layout] <title> #创建文章
hexo new page <title> #生成新页面
hexo generate #自动根据当前目录下文件,生成静态网页
-d #deploy after
-w #查看文件变化
hexo publish [layout] <filename> #发布草稿
hexo server #运行本地服务
-p #端口
-s #只生成静态文件
-l #生成log
--draft 连草稿也生成预览
hexo deploy
-g #generate before
hexo render <file1> [file2] ...
-o #output destination
hexo migrate <type> #Migrates content from other blog systems.
hexo clean #Cleans the cache file (db.json) and generated files (public).
hexo list <type> #Lists all routes.

Read More

Share   |  Updated:

编写可维护javascript

for-in:对象属性遍历

  • 循环不仅遍历对象实例属性,还遍历从原型继承来的属性;若只想遍历该对象属性

var prop,obj={a:1.b:2,c:3};
for (prop in obj) {
    if (obj.hasOwnProperty(prop)) {
        //遍历只属于该对象的属性
    }
}

in:判断属性在对象中是否存在

var obj={
    cnt:0,
    related:null
};
if("cnt" in obj){
    //do some code
}

instanceof:判断一个变量是否某个js 对象( 非dom对象)的实例

typeof:判断运算数的类型
  • 返回number,boolean,string,function,object,undefined

处理错误

  • 自定义错误

function MyError(msg){
	this.msg=msg;
}
MyError.prototype = new Error();
  • try-catch-finally

基于对象继承:不破坏源对象

var person={
  age:11,
  name:'virgil'
};
var myPerson=Object.create(person);
myPerson.age=12;
console.log(myPerson.age,person.age);//12 11

基于类型继承

function Person(name){
	this.name;
}

function Author(name){
	Person.call(this,name); //继承构造器
}

Author.prototype = new Person(); //原型继承

禁止修改

  • preventExtension #防止扩展
  • seal #密封
  • freeze #冻结

引用

  • Array非固定长度,可以动态增删,因此数组是引用类型

      var arr=[1,2,3];
      var arrRef=arr;
      arr.push(4);
      console.log(arrRef);
    

作用域链

  • 全局作用域中,该链中有且只有一个对象,即全局对象
  • 函数中,有两个对象,先会被访问到的是调用对象,然后是全局对象

《JavaScript核心概念及实践》

Questions? Leave a comment below or email: newbvirgil@gmail.com
Share   |  Updated:

PHP assignment and references (copy-on-write)

php的赋值与引用

References are easy to get wrong: it is not always “two names, one block of memory.” Reading the Zend engine behaviour (and PHP Kernel and Best Practices) helped clarify copy-on-write (COW) versus real references.

Copy-on-write

  • PHP variables carry is_ref and refcount to track references and sharing.
  • With $a = $b; (not a reference), if the values are the same string/value, Zend may share storage and bump refcount instead of copying immediately.
  • When $b is written, PHP allocates new storage, sets refcount to 1, and detaches from the shared value.

Read More

jquery 和 html5

Jquery

选择器

  • prev>child
  • prev+next
  • prev~siblings
  • [attr^=”val”] : 开头为val的属性
  • [attr*=”val”] : 包含val的属性
  • [attr~=”val”] : 将attr以空格分隔后,含有val片段的属性
  • [attr|=”val”] : 为val或val-开头的属性

Read More

Share   |  Updated:

JavaScript data types — cheat sheet

js数据类型

Types

String vs String object

  • Primitives coerce to object wrappers when needed.
1
2
3
4
5
var a = new String('a');
var b = new String('a');
a == b; // false (different objects)
a === 'a'; // false (object vs primitive)
String('a') === 'a'; // true
  • Strings are immutable; methods return new values.
1
2
3
var a = 'abc';
a[0] = 'A';
console.log(a); // still 'abc'

Read More

Share   |  Updated:

JavaScript functions, closures, and misc browser APIs

js函数和闭包

Scope

Function scope

Inside a function, hoisting means declarations are visible throughout the function body regardless of source order.

Block scope

Historically var was function-scoped, not block-scoped:

1
2
3
4
{
for (var i = 0; i < 10; i++) { /* ... */ }
// i === 10 here
}

Use let / const for block scope.

Read More

Share   |  Updated:

PHP regex cheat sheet — metacharacters, groups, lookaround, modifiers

正则速查

Regular expressions

  • Metacharacters

    • . — any character except newline
    • \w — word characters (letters, digits, underscore; may include CJK depending on engine/settings)
    • \s — whitespace
    • \b — word boundary
    • * — 0 or more of the preceding atom
    • + — 1 or more
    • ? — 0 or 1
    • ^ — start of string (or negation inside [])

      example: capture anchor text (non-greedy inner match)
              $reg='#<a[^>]*>([^<>]*)<\/a>#';
              $str="<a href='ssss'>some</a>other<a href='ddd'>some</a>";
              $res=preg_match_all($reg,$str,$m);
    

Read More

Shuffle an array without repeating the same permutation (within 100 tries)

对数组元素打乱输出(不重复随机串)

From a 2014 written test:


Q: Given [1,2,3,4,5], emit a shuffled order. Within 100 draws, no permutation may repeat.

My first idea: Math.random() sort plus a closure tracking seen key strings. Later I tracked the value string instead and borrowed shuffle ideas from others.

A:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function random(a, b) {
return Math.random() > 0.5 ? -1 : 1;
}

var uniqueArr = (function () {
var arr = [];
return function (uniqueStr) {
for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] == uniqueStr) return false;
}
arr.push(uniqueStr);
return arr;
};
})();

function randomSort(arr) {
var res = arr.sort(random);
var keys = res.join('');
if (uniqueArr(keys)) {
return res;
}
return randomSort(arr);
}

var arr = [1, 2, 3, 4, 5];
randomSort(arr);

This comparator is not a uniform shuffle (use Fisher–Yates for fair shuffles). The puzzle was about unique permutations, not unbiased randomness. Improvements welcome.

Questions? Leave a comment below or email: newbvirgil@gmail.com

html5拖拽2-chrome拖拽下载

  1. 接着上一文章,chrome中实现了文件的拖拽下载,和桌面互动的效果非常好。
    主要要求是在dataTransfer中设定文件的信息,信息需按以下格式:

    'MIMETYPE:文件名:文件URL'
    

    我们可以这样设置文件元素:

    <a id="mp3" 
        href="http://e.com/f.mp3" 
        draggable='true' 
        data-downloadurl="audio/mpeg:f.mp3:http://e.com/f.mp3">you can download this mp3 to desktop in chrome by drag
    </a>
    
  2. 接下来问题是获取自定义属性,即data-*,使用dataset(html5属性)

    var url=document.getElementById("mp3").dataset.downloadurl;
    
  3. 最后就是绑定数据了:

    document.getElementById("mp3").ondragstart=function(e){
                e.dataTransfer.setData('DownloadURL',e.target.dataset.downloadurl);
            };
    

    可以拖动下载到桌面了,记得是在chrome中啊。

Questions? Leave a comment below or email: newbvirgil@gmail.com
Share   |  Updated:

HTML5 drag and drop API

html5的拖拽api

Overview

Two common cases:

  • Dragging in-page elements or data
  • Dragging files from the OS into the page

You need:

  • A source (draggable element)
  • A target (drop zone)

Read More