您正在查看: 2016年1月

js对象的那点事儿

原型

原型的概念以及访问

JS中原型就是一个对象,它用于某个对象从其他对象中继承属性。 怎么理解这句话呢:例如,对象b想有一个.x属性。如果对象a有.x属性,那么让b的原型等于a,则b就也有了.x属性。注意b的.x属性本质上就是a的.x属性。

每个对象都有一个原型。原型本身也是对象。这意味着原型本身也含有一个原型。当然这样下去似乎无穷无尽,自然有一个原型链的顶层对象,这个对象叫Object。这个对象是一个例外,它没有原型。

原型有几种方式可以访问

// ECMA5 标准 IE<=8失败
Object.getPrototypeOf(a);
// IE下失败,其他都可以
a.__proto__;
// 所有浏览器都可以,但是这有可能会出问题,下面讲对象的构造的时候会说明
a.constructor.prototype

bash snippets

Little Useful Functions

The Real Dir of Script itsef

sometimes we need to change dir to the real path of a script and do something. But this little thing is very hard to make it works in lots of Linux Distribution and Mac OS X. The snippets below is work on CentOS, Ubuntu and Mac OS X 10.10.

#!/bin/sh
# read link 
_script="$(readlink -n ${0})"
if [[ $_script = '' ]]; then
    # when call script without symbolic, readlink will return empty string
    pushd `dirname $0` > /dev/null
    _script=`pwd`
    popd > /dev/null
else
    _script="$(dirname $_script)"
fi
echo 'the real path is ' $_script

command usage

echo color

16color syntax:

# \033 is the Escape symbol in ASCII, it can also written in \x1b
echo -e "\033[${attr};${clbg};${clfg}m${renderText}\033[0m"
# -e explain as use string escape
# if you let \033[ in a variable, -e is not necessary. such as
start="\x1b["
end="\x1b[0m"
echo "${start}${attr};${clbg};${clfg}m${renderText}${end}"

which attr is control number, clbg is background color, clfg is foreground color.
The detail is in tip_colors_and_formatting