Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP & JavaScript & CSS Coding Style 探討

Bo-Yi Wu
April 27, 2016

PHP & JavaScript & CSS Coding Style 探討

分別介紹 PHP & JavaScript & CSS Coding Style 工具

Bo-Yi Wu

April 27, 2016
Tweet

More Decks by Bo-Yi Wu

Other Decks in Technology

Transcript

  1. <?php try { // try body } catch (FirstExceptionType $e)

    { // catch body } catch (OtherExceptionType $e) { // catch body } 30  
  2. 安裝方式   •  wget http://get.sensiolabs.org/php-cs- fixer.phar -O php-cs-fixer •  $

    sudo chmod a+x php-cs-fixer •  $ sudo mv php-cs-fixer /usr/local/bin/php- cs-fixer 36  
  3. 缺陷   •  需要經過  compiler 才能變成  CSS 檔案   • 

    要學習  sass/scss/less 新語法   •  專案成長後,需要 compiler   時間越久   •  無法支援 css  lint 檢查語法   52  
  4. Plugin •  Use Future CSS, Today – autoprefixer, postcss-cssnext •  Better

    CSS Readability – precss, postcss-sorting •  Images and Fonts – postcss-sprites 56  
  5. Usage gulp.task('css', () => { let postcss = require('gulp-postcss'); return

    gulp.src('src/*.css') .pipe( postcss([ plugin1, plugin2 ]) ) .pipe( gulp.desc('build/') ); }); 57  
  6. Like Sass/Less PostCSS .block { &_title { font-size: 1.2em; }

    } CSS .block_title { font-size: 1.2em; } 59  
  7. Fallbacks PostCSS .foo { opacity: 0.8; } CSS .foo {

    opacity: 0.8; filter: alpha(opacity=80)\9; } 60  
  8. Plugin for CSS lint .foo { margin-top: 10px; margin: 0

    auto; } foo.css:3:5: margin overrides margin-top. 62  
  9. PostCSS vs Gulp PostCSS •  Parse –  Transform –  Fallbacks

    –  Minify Gulp •  Parse –  Transform •  Parse –  Fallbacks •  Parse –  Minify 63  
  10. CSS Style Lint gulp.task('lint:css', function () { return gulp.src('src/*.less') .pipe(

    postcss([ require('stylelint'), require('postcss-reporter'), ], { syntax: require('postcss-less') }) ); }); 69  
  11. Properties var luke = { jedi: true, age: 28 };

    // bad var isJedi = luke['jedi']; // good var isJedi = luke.jedi; 77  
  12. Arrow Functions [1, 2, 3].map(function(n) { return n * 2;

    }, this); // -> [ 2, 4, 6 ] [1, 2, 3].map(n => n * 2); // -> [ 2, 4, 6 ] 83  
  13. function Person() { var self = this; self.age = 0;

    setInterval(function growUp() { self.age++; }, 1000); } var p = new Person(); 86  
  14. Block Scoping Functions var a = 5; var b =

    10; if (a === 5) { let a = 4; var b = 1; console.log(a); // 4 console.log(b); // 1 } console.log(a); // 5 console.log(b); // 1 var a = 5; var b = 10; if (a === 5) { (function () { var a = 4; b = 1; console.log(a); // 4 console.log(b); // 1 })(); } console.log(a); // 5 console.log(b); // 1 88  
  15. Template var user = { name: 'Caitlin Potter' }; console.log('Thanks

    for V8, ' + user.name + '.'); var user = {name: 'Caitlin Potter'}; console.log(`Thanks for V8, ${user.name}.`); 89  
  16. Computed Property Names var prefix = 'foo'; var test= {

    [prefix + 'bar']: 'hello', [prefix + 'baz']: 'world' }; console.log(test['foobar']); // -> hello console.log(test['foobaz']); // -> world var prefix = 'foo'; var test= {}; test[prefix + 'bar'] = 'hello'; test[prefix + 'baz'] = 'world'; console.log(test['foobar']); // -> hello console.log(test['foobaz']); // -> world 90  
  17. Destructuring Assignment function f(x, y) { if (y === undefined)

    { y = 12; } return x + y; } f(3) === 15; function f(x, y = 12) { return x + y; } f(3) === 15; 91  
  18. Classes function Hello(name) { this.name = name; } Hello.prototype.hello =

    function hello() { return 'Hello ' + this.name; }; Hello.sayHelloAll = function () { return 'Hello everyone!'; }; class Hello { constructor(name) { this.name = name; } hello() { return 'Hello ' + this.name; } static sayHelloAll() { return 'Hello everyone!'; } } 92  
  19. Module var math = require('lib/math'); console.log('2π = ' + math.sum(math.pi,

    math.pi)); import math from 'lib/math'; console.log('2π = ' + math.sum(math.pi, math.pi)); 93  
  20. Property Method Assignment var object = { value: 42, toString:

    function toString() { return this.value; } }; var test= { value: 42, toString() { return this.value; } }; 94  
  21. Rest Parameters function f(x) { var y = []; y.push.apply(y,

    arguments) && y.shift(); // y is an Array return x * y.length; } function f(x, ...y) { // y is an Array return x * y.length; } console.log(f(3, 'hello', true) === 6); 95  
  22. Spread Operator function f(x, y, z) { return x +

    y + z; } f.apply(null, [1, 2, 3]) === 6; function f(x, y, z) { return x + y + z; } f(...[1,2,3]) === 6; 96