只要執行三行指令即可
```bash
sudo add-apt-repository ppa:webupd8team/atom
sudo apt-get update
sudo apt-get install atom
```
2016年4月25日 星期一
Linux使用php7連接mssql,並設定utf-8
使用php7要連接mssql要安裝的套件為pdo_dblib
```bash
# centos
yum install php70w-pdo_dblib.x86_64
# ubuntu
sudo apt-get install freetds-bin php-sybase
```
接下來我們更改freetds的設定檔,裝連線設定為utf8
```bash
# centos
vi /etc/freetds.conf
# ubuntu
sudo vi /etc/freetds/freetds.conf
```
加入兩行設定即可
```ini
# centos
tds version = 7.2
client charset = UTF-8
# ubuntu
tds version = 7.1
client charset = UTF-8
```

設定完成後連線到mssql charset就會是utf8
如果還是顯示為亂碼的話則可以利用tsql的指令來debug
```bash
tsql -S [ip] -U [username] -P [password]
```

2016年4月3日 星期日
es6如何拓增prototype
寫es6時要增加Object的prototype
已經和以往的寫法大為不同了
所以就來介紹一下方法吧
```js
// $.inArray 模擬 Array.includes
import $ from 'jquery';
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value(...args) {
return $.inArray(...args, this) !== -1;
}
});
}
```
利用jQuery Promise模擬Native Promise
我們在寫es6時,會用到一些es6才有的函式或物件
但如果可以使用jQuery的一些內建函式來模擬的話
我們就不需要import 'babel-polyfill'
這樣產出來的javascript檔案就會少個90k左右
以下為程式碼
```js
'use strict';
import $ from 'jquery';
if (!window.Promise) {
class Promise {
constructor(callback) {
this.deferred = $.Deferred();
callback((o) => {
this.deferred.resolve(o);
}, (o) => {
this.deferred.reject(o);
});
this.promise = this.deferred.promise();
}
then(resolve, reject) {
this.promise.done(resolve);
this.promise.fail(reject);
return this;
}
catch(reject) {
this.promise.fail(reject);
return this;
}
}
window.Promise = Promise;
}
```
解決babel6 class extends 在 ie10 以下版本super無法呼叫 parent constructor
先直接看程式碼
```js
class A {
constructor() {
console.log('A');
}
}
class B extends A {
constructor() {
super();
}
}
new B();
```
這是一個很簡單物件繼承,預期console內輸出 "A"
實際做了測試之後發現在ie11, chrome, firefox都能正常輸出
但在ie9, ie10卻完全失效
後來查了原因後原來Object.getPrototypeOf在es5及es6的功能完全不同
才造成super失效
在github上已經有人提出兩種出解決方案
1.設定babel的plugins
```json
{
"presets": ["react", "es2015"],
"plugins": [
["transform-es2015-classes", { "loose": true }],
"transform-proto-to-assign"
]
}
```
2.修改Object.getPrototypeOf 來自[https://github.com/seznam/IMA.js-babel6-polyfill](https://github.com/seznam/IMA.js-babel6-polyfill "polyfill")
```js
(function() {
var testObject = {};
if (!(Object.setPrototypeOf || testObject.__proto__)) {
var nativeGetPrototypeOf = Object.getPrototypeOf;
Object.getPrototypeOf = function(object) {
if (object.__proto__) {
return object.__proto__;
} else {
return nativeGetPrototypeOf.call(Object, object);
}
}
}
})();
```
2016年3月13日 星期日
php composite + __call
```php
class Weapon
{
protected $name;
protected $attack = 0;
protected $addions = [];
public function __construct($name, $attack, $addions = [])
{
$this->name = $name;
$this->attack = $attack;
$this->addions = $addions;
}
public function setAttack($attack)
{
$this->attack = $attack;
}
public function attack()
{
$attack = $this->attack;
foreach ($this->addions as $addion) {
$attack += $addion->attack();
}
return $attack;
}
public function __call($method, $parameters)
{
foreach ($this->addions as $addion) {
if (method_exists($addion, $method) === true) {
return call_user_func_array([$addion, $method], $parameters);
}
}
die('error');
}
}
abstract class Addion
{
protected $attack = 0;
public function attack()
{
return $this->attack;
}
}
class ToxicAddion extends Addion
{
protected $attack = 15;
public function toxic()
{
echo 'toxic';
}
}
class LightingAddion extends Addion
{
protected $attack = 10;
public function lighting()
{
echo 'lighting';
}
}
$sword = new Weapon('劍', 10, [
new ToxicAddion(),
new LightingAddion(),
]);
echo $sword->attack();
echo $sword->toxic();
echo $sword->lighting();
// output
// 35
// toxic
// lighting
```
PHP decorator + __call
```
class Weapon
{
public function attack()
{
return 10;
}
}
abstract class Decorator
{
protected $weapon;
public function __construct($weapon)
{
$this->weapon = $weapon;
}
abstract public function attack();
public function __call($method, $parameters)
{
return call_user_func_array([$this->weapon, $method], $parameters);
}
}
class ToxicDecorator extends Decorator
{
public function attack()
{
return $this->weapon->attack() + 15;
}
public function toxic()
{
echo 'toxic';
}
}
class LightingDecorator extends Decorator
{
public function attack()
{
return $this->weapon->attack() + 5;
}
public function lighting()
{
echo 'lighting';
}
}
$weapon = new Weapon();
$weapon = new ToxicDecorator($weapon);
$weapon = new LightingDecorator($weapon);
echo $weapon->attack();
echo $weapon->toxic();
echo $weapon->lighting();
// output
// 30
// toxic
// lighting
```
訂閱:
文章 (Atom)