Coffeescript。 そして再び彼について。 アメニティの概要

CoffeeScriptは、いわば、JavaScriptに変換できる高レベル言語です。



githubで言及されている彼の相棒のサポートでJeremy Ashkenasという名前の単純な男によって設立されました。 Rubyに触発されて、男は私たちの生活を簡素化することを決定し、彼を作成しました! CoffeeScript!







目標





この言語の目的と目的は、JavaScriptを使用する開発者の頭痛を解消することです。 JavaScriptに精通している人なら誰でも、継承のような魔法のような急成長中のコードを維持すること、オブジェクトのコンテキストをコールバックメソッドに渡すことなどがどれほど難しいかを知っています。 そして、JavaScriptに自然なプロトタイプ継承を使用してクラスに分割された複数ページのネイティブコードを書く神経を持っている人たちは、自分たちが喜んで自分自身を撃つことができます。 おいおい、CoffeeScriptはそれを簡単でシンプルにします:



  1. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  2. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  3. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  4. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  5. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  6. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  7. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  8. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  9. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  10. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  11. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  12. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  13. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  14. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  15. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  16. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  17. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  18. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  19. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()



  20. class Animal name: "animal" move: (meters) -> alert (@name + " moved " + meters + "m." ) class Snake extends Animal constructor: (name) -> @name: name move: -> alert ( "Slithering..." ) super ( 5 ) class Horse extends Animal constructor: (name) -> @name: name move: -> alert ( "Galloping..." ) super ( 45 ) sam: new Snake ( "Sammy the Python" ) tom: new Horse ( "Tommy the Palomino" ) sam.move() tom.move()







どのCoffeeScriptが最低価格で契約に署名したかについて心配する必要はありません(取引を行う瞬間まで)が、出力でネイティブの高速実行コードを取得することを確認してください:



  1. 関数 (){
  2. var Animal、Horse、Snake、Sam、Tom;
  3. var __extends = function (child、parent){
  4. var ctor = function (){};
  5. ctor.prototype = parent.prototype;
  6. 子.__ superClass__ = parent.prototype;
  7. child.prototype = new ctor();
  8. child.prototype.constructor = child;
  9. };
  10. 動物= 関数 (){};
  11. Animal.prototype.move = function (meters){
  12. 警告を返すこの .name + "moved" + meter + "m。" );
  13. };
  14. スネーク= 関数 (名前){
  15. this .name = name;
  16. これを返す ;
  17. };
  18. __extends(ヘビ、動物);
  19. Snake.prototype.move = function (){
  20. アラート( 「スリザリング...」 );
  21. return Snake .__ superClass __。move.call( this5 );
  22. };
  23. Horse = function (name){
  24. this .name = name;
  25. これを返す ;
  26. };
  27. __extends(馬、動物);
  28. Horse.prototype.move = function (){
  29. alert( "ギャロッピング..." );
  30. return Horse .__ superClass __。move.call( this45 );
  31. };
  32. sam = new Snake( "Sammy the Python" );
  33. tom = new Horse( "パロミノトミー" );
  34. sam.move();
  35. tom.move();
  36. })();




すべてがその環境で実行されるため、名前の競合を心配しないでください。 しかし、そのような「パッケージ」からクラスまたは変数を「共有」する必要がある場合、それらへの参照をグローバル名前空間の変数に割り当てることは費用がかかりません。



ドキュメントについて。





ここで CoffeeScriptの使用方法について英語で読んでください 。 私の意見では、すべてが例とともに明確に説明されています。



特徴





すべてのコーヒースクリプトは、他のコーヒースクリプトを「損なわない」ように、コーヒーメーカーで「作成」されます。 個人的には、これは正しいと思います。



短いラムダはスーパーです。 そして、私が本当に気に入ったのは、コールバックハンドラーへのコンテキストの転送です。



  1. クラス データ
  2. manyData:null
  3. コンストラクター:->
  4. 遅延:新しい遅延()
  5. @initData(遅延)
  6. SomeClass.fillData(遅延)
  7. initData:(遅延)->
  8. deferred.addCallback:(データ)=>
  9. @manyData:データ
  10. deferred.addCallback:(エラー)->
  11. アラート (error.name + ":" + error.message)
  12. 新しいデータ ()




出力:




  1. var Data;
  2. データ= 関数 (){
  3. var deferred;
  4. deferred = new Deferred();
  5. this .initData(遅延);
  6. SomeClass.fillData(遅延);
  7. これを返す ;
  8. };
  9. Data.prototype.manyData = null ;
  10. Data.prototype.initData = function (deferred){
  11. deferred.addCallback =( function (__this){
  12. var __func = function (data){
  13. this .manyData = data;
  14. この .manyDataを返します。
  15. };
  16. returnfunction (){
  17. return __func.apply(__ this、arguments);
  18. });
  19. })( this );
  20. deferred.addCallback = function (error){
  21. 警告を返す (error.name + ":" + error.message);
  22. };
  23. return deferred.addCallback;
  24. };
  25. 新しいデータ();




コーヒーのコードがより適切に見えることに同意します。



var



を介して変数を宣言することを心配する必要がvar



function



という言葉を完全に忘れる必要がなくなりました-CoffeeScriptでは禁止されています。 ただし、CoffeeScriptコーデックに通常のJavaScriptを挿入する必要がある場合は、文字` `



でラップするだけです:



  1. hi: `関数(){
  2. return [document.title、 "Hello JavaScript" ] .join( ":" );
  3. } `
  4. `var getSomeValue = function(){...}`




その結果:




  1. var hi;
  2. hi = 関数 (){
  3. return [document.title、 "Hello JavaScript" ] .join( ":" );
  4. };
  5. var getSomeValue = function (){...};




しかし、これは、絶望の極端な場合にのみ必要になると思います。



チップス





CoffeeScriptはさまざまな種類の言語構成要素にアクセスできますが、残念ながらネイティブJavaScriptでは使用できません。プロジェクトのWebサイトからいくつかの例を紹介します。



CoffeeScript




  1. コレステロール: 127
  2. 健康: 200 >コレステロール> 60




Javascript




  1. コレステロールの健康、健康;
  2. コレステロール= 127 ;
  3. 健康=( 200 >コレステロール)&&(コレステロール> 60 );




CoffeeScript




  1. theBait: 1000
  2. theSwitch: 0
  3. [theBait、theBait]:[theSwitch、theBait]




Javascript




  1. var _a、theBait、theSwitch;
  2. theBait = 1000 ;
  3. theSwitch = 0 ;
  4. _a = [theSwitch、theBait];
  5. theBait = _a [ 0 ];
  6. theSwitch = _a [ 1 ];




CoffeeScript




  1. 番号:[ 0 .. 9 ]
  2. threeToSix:数字[ 3 .. 6 ]
  3. コピー:数字[ 0 ... numbers.length]




Javascript




  1. var copy、numbers、threeToSix;
  2. 数字=( 関数 (){
  3. a = []; forvar i = 0 ;( 0 <= 9 ?i <= 9 :i> = 9 );( 0 <= 9 ?i + = 1 :i-= 1 ))a.push(i);
  4. を返します;
  5. })。call( this );
  6. threeToSix = numbers.slice(3、6 + 1 );
  7. copy = numbers.slice( 0 、numbers.length);




CoffeeScript




  1. yearsOld:{max: 10 、ida: 9 、tim: 11 }
  2. 年齢:子供の場合、年齢
  3. 子+ "is" +年齢




Javascript




  1. var _a、_b、年齢、年齢、子供、yearsOld;
  2. var __hasProp = Object.prototype.hasOwnProperty;
  3. yearsOld = {
  4. 最大: 10
  5. ida: 9
  6. tim: 11
  7. };
  8. 年齢=( 関数 (){
  9. _a = []; _b = yearsOld;
  10. for (_bの子){ if (__hasProp.call(_b、child)){
  11. 年齢= _b [子];
  12. _a.push(子+ "is" +年齢);
  13. }}
  14. return _a;
  15. })();




CoffeeScript




  1. 年:[ 2000 .. 2010 ]
  2. 何年も
  3. 「年」 +年




Javascript




  1. var _a、_b、_c、年、年;
  2. 年=( 関数 (){
  3. a = []; forvar i = 2000 ;( 2000 <= 2010 ?i <= 2010 :i> = 2010 );( 2000 <= 2010 ?i + = 1 :i-= 1 ))a.push(i);
  4. を返します;
  5. })。call( this );
  6. _b =年;
  7. for (_a = 0 、_c = _b.length; _a <_c; _a ++){
  8. 年= _b [_a];
  9. 「年は」 +年。
  10. }




まとめ





もちろん、本格的な開発には、強調表示や自動補完に十分な人気のあるプラグインはありませんが、これらの一時的な問題を無視すると、CoffeeScriptは本質的に複雑なJavaScriptアプリケーションの作成を簡素化します。



CoffeeScriptの大きな利点は、簡単に複雑なコードを作成できることです。これにより、実行時に、クラシッククラスをエミュレートするさまざまなライブラリと、誰もが同意できるECMA標準の特定の実装を接続する必要がなくなり、スクリプトの速度が大幅に向上します。



そして、誰もが好みに応じて開発および展開モードの組織を選択しますが、これはこのトピックのタスクではありません。



ご清聴ありがとうございました。



All Articles