2016年4月25日 星期一

ubuntu下安裝atom editor

只要執行三行指令即可 ```bash sudo add-apt-repository ppa:webupd8team/atom sudo apt-get update sudo apt-get install atom ```

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 ``` ![freetds config](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfZxvhvTwGAuP2pwtB80Bbypwb54mp8uAIPe5kAPEB8Swk4l5BFpt0ZncIr_SMZ43RW5ibnF7SOP_AeE5D7PX5JFbk2uBPZkK_Q0jL0MFFlMXRkmgJeUNFv17CszpfALpdfu-HJ23aKN92/s1600/Image+3.png) 設定完成後連線到mssql charset就會是utf8 如果還是顯示為亂碼的話則可以利用tsql的指令來debug ```bash tsql -S [ip] -U [username] -P [password] ``` ![tsql](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhplyPJmyyXaqfmXpo1D9Tl72GhIOrn_P2QgjwASD3-QNn_3YUyVBsCX6FPv0TdYC6D9hhYd2JIVR8OEHZXnBErLy2004Kmsah5y1YOqCoZ5WtiPL5sbYzWiB-6dxaPz3OKny6Ji2qJw-Vf/s1600/Image+2.png)

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); } } } })(); ```