
SugarJSとは何ですか?
SugarJSは、有用なメソッドでネイティブオブジェクトを拡張するオープンソース(MITライセンス)Javascriptライブラリです。 コードの表現力を高める直感的で控えめなツールになるように設計されているため、少ないコードでより多くのことを実行し、ルーチンについて考える必要が少なくなります。
実際、彼らは非常に有益でシンプルなサイトを持っています-すべてがすでによく書かれています
主なポイントの概要を説明します。
簡単に...
- 便利で直感的なメソッドでネイティブのJavascript要素を拡張します。
- わかりやすく使いやすい。
- ネイティブブラウザー実装にメソッドがある場合、SugarJSメソッドは使用されません。
- それは、 テストの巨大なセットを持っているテストシステムを持っています。
- 日付と時刻を扱う可能性を広げ、いくつかの言語(ロシア語)で書式を提供します。
- 他のフレームワークやサードパーティのコードとうまく調和しています。
- モジュール性 、必要なパッケージのみを含めることができます。
- 完全に互換性のあるECMAScript。
- モバイルを含むすべての主要なブラウザのサポートを宣言しました。
- Node.jsをサポートします。
- 便利で便利なドキュメントがあります。
に興味がありますか? 猫へようこそ。
JSの詳細については、ドキュメントの対応するセクションをご覧ください。
標準のSugarJS配信には一部の機能が含まれていない場合があることに注意してください。 サイトのコンフィギュレータを使用して、必要なモジュールを接続します(ドキュメントのメソッド名の右側にあるアイコンに注意してください)。
SugarJSの主要な機能は7つのセクションに分かれています。ここでは、私の意見で最も興味深い機能を紹介します。
文字列 -文字列を使用
'dopamine'.insert('e', 3);
'carve me up!'.from(2);
'carve me up!'.to(2);
'carve me up!'.first(2);
'carve me up!'.last(2);
'jumpy'.has('py');
'add me!'.add('to ', 4);
'[you] escape <me>'.escapeHTML();
' !'.hasCyrillic();
'welcome'.pad(' ', 1).pad('-', 3);
'hell, no!'.parameterize();
'anVzdCBnb3QgZGVjb2RlZA=='.decodeBase64();
'{n} and {r}'.assign({ n: 'Cheech' }, { r: 'Chong' });
数字 -数字を操作する
(125.425).round(2);
(4235000).format();
(50).pad(5);
(5).daysAfter('Wednesday');
(1000000).abbr();
(75).minutes().duration('ru');
(23654).hex();
(17).isEven();
(8).times(function(i) {
(125.425).round(-2);
配列 -配列を操作します
['a','b','c'].forEach(function(el) {
['a','b','c'].indexOf('c');
['rocksteady','and','bebop'].map('length');
['rocksteady','and','bebop'].sortBy('length');
['rocksteady','and','bebop'].findAll(/o/);
['rocksteady','and','bebop'].first(1);
['rocksteady','and','bebop'].from(1);
['three','two','one'].groupBy('length');
[1,65,2,77,34].average();
[1,2].add([2,3]);
[1,2].subtract([2,3]);
[1,2].intersect([2,3]);
[1,2].union([2,3]);
Object.extended({ broken: 'wear' }).keys();
Object.extended({ broken: 'wear' }).values();
Object.keys({ broken: 'wear' });
Object.has({ foo: 'bar' }, 'foo');
Object.merge({a:1},{a:2}, false, function(key, a, b) { return a + b; });
機能 -機能の使用
(function(a) {
(function() {
[1,2,3].each(function() {
var fn = (function() {
RegExp.escape("oh not /b/, aren't those guys gone?");
/broken/.setFlags('gim');
/broken/im.addFlag('g');
/broken/gi.removeFlag('g');
日付 -日付を操作する
Date.create('2002-06-15');
Date.create('June 15, 2002');
Date.create('today');
Date.create('2 days ago');
Date.create('the last day of 1998');
Date.create('先週金曜日','ja');
Date.create('25 ', 'ru');
Date.create('二十五日', 'zh-CN');
Date.create('il ya une semaine', 'fr');
Date.create().format('{12hr}:{mm}{tt} on {Weekday}');
Date.create().iso();
Date.create('3200 seconds ago').relative();
Date.create('3200 seconds ago').relative('zh-TW');
Date.create('3200 seconds ago').relative('de');
Date.create('3200 seconds ago').relative('ko');
Date.create().is('tuesday');
Date.create().is('the 7th of June');
Date.create().addMonths(2);
コード例
getLatestTweets(function(tweets) { var users = tweets.map('user').unique(), total = users.sum('statuses_count').format(), top = users.max('followers_count'), followers = top.followers_count.format(), started = Date.create(top.created_at); return users.length + ' users with a total of ' + total + ' tweets.\n' + top.screen_name + ' is the most popular with ' + followers + ' followers\n' + 'and started tweeting ' + started.relative() + '.'; });
結果 :
20 users with a total of 203,499 tweets.
Hi_Im_Dopee is the most popular with 744 followers
and started tweeting 1 year ago.
私はこのライブラリをjQuery、Knockout、Bootstrapで1か月以上使用しており、その操作に問題はありませんでした。