IT戦記

プログラミング、起業などについて書いているプログラマーのブログです😚

Prototype.js 1.5.0 がこの一週間めちゃめちゃ更新してるので、新しい機能をまとめてみた

最近の Prototype.js

LL Ring でも言ったけど、まったくもって 4 ヶ月間更新なしだった。。。

しかし!

ここ、 1 週間でこんなに更新があった!
http://dev.rubyonrails.org/changeset?new=spinoffs%2Fprototype%404950&old=spinoffs%2Fprototype%404236

ということで、新しい機能まとめ

DOM 要素に observe, stopObserving

Element.Methods に observe が追加された。

var handler = alert.bind(null, 'クリックありがとう!');
$('target').observe('click', handler); // うーん、直感的!
$('target').stopObserving('click', handler); // キャンセル!直感的!
getElementsByClassName, getElementsBySelector

Element.Methods に getElementsByClassName と getElementsBySelector が追加された。

$('target').getElementsByClassName('hoge'); // とか
$('target').getElementsBySelector('ul.friendList li.dummy a'); // とかできる
Xxx.Methods オブジェクト

Element.Methods オブジェクトと同様に Form.Methods 及び Form.Element.Methods が追加された。
これによって、 Element.extend 関数の実行で FORM 要素には Form.Methods の関数が INPUT, TEXTAREA, SELECT 要素には Form.Element.Methods が追加されるようになった。

var text = $('target'/*FORM要素*/).serialize(); // とか
$('target'/*INPUT要素とか*/).activate(); // とかできる
return element

Xxx.Methods 系のメソッドで、復帰値(return) が無かった関数に return element がついかされた。

$('target'/*INPUT要素*/).show().activate(); // とかできる(表示して選択)
Object.values, Object.keys

keys は便利そうだ

Object.keys(document); // 結果は以下
/*
["getElementsByClassName","xmlVersion","createElement","createTextNode","getElementsByTagName","styleSheets","title","referrer","baseURI","compareDocumentPosition","textContent","isSameNode","lookupPrefix","isDefaultNamespace","lookupNamespaceURI","isEqualNode","getFeature","setUserData","getUserData","DOCUMENT_POSITION_DISCONNECTED","DOCUMENT_POSITION_PRECEDING","DOCUMENT_POSITION_FOLLOWING","DOCUMENT_POSITION_CONTAINS","DOCUMENT_POSITION_CONTAINED_BY","DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC","nodeName","nodeValue","nodeType","parentNode","childNodes","firstChild","lastChild","previousSibling","nextSibling","attributes","ownerDocument","insertBefore","replaceChild","removeChild","appendChild","hasChildNodes","cloneNode","normalize","isSupported","namespaceURI","prefix","localName","hasAttributes","doctype","implementation","documentElement","createDocumentFragment","createComment","createCDATASection","createProcessingInstruction","createAttribute","createEntityReference","importNode","createElementNS","createAttributeNS","getElementsByTagNameNS","getElementById","ELEMENT_NODE","ATTRIBUTE_NODE","TEXT_NODE","CDATA_SECTION_NODE","ENTITY_REFERENCE_NODE","ENTITY_NODE","PROCESSING_INSTRUCTION_NODE","COMMENT_NODE","DOCUMENT_NODE","DOCUMENT_TYPE_NODE","DOCUMENT_FRAGMENT_NODE","NOTATION_NODE","URL","body","images","applets","links","forms","anchors","cookie","close","getElementsByName","width","height","alinkColor","linkColor","vlinkColor","bgColor","fgColor","domain","embeds","getSelection","write","writeln","clear","captureEvents","releaseEvents","routeEvent","compatMode","plugins","designMode","execCommand","execCommandShowHelp","queryCommandEnabled","queryCommandIndeterm","queryCommandState","queryCommandSupported","queryCommandText","queryCommandValue","characterSet","dir","contentType","lastModified","getBoxObjectFor","setBoxObjectFor","createEvent","preferredStylesheetSet","defaultView","createRange","createNodeIterator","createTreeWalker","getAnonymousNodes","getAnonymousElementByAttribute","addBinding","removeBinding","getBindingParent","loadBindingDocument","removeEventListener","dispatchEvent","inputEncoding","xmlEncoding","xmlStandalone","strictErrorChecking","documentURI","adoptNode","domConfig","normalizeDocument","renameNode","createExpression","createNSResolver","evaluate"]
*/ 
Object.values({a: 1, b: 2}); // 2
Enumerable.reduce

要素ひとつの Enumerable (配列とか)を値にしてくれる。絶対配列形式で復帰してくる関数なんかに便利かも。

[1].reduce(); // 1
$A(document.getElementsByTagName('body')).reduce(); // body 要素はひとつだから配列を復帰しない
Enumerable.uniq

Enumerable (配列とか)をユニーク化する。

[1, 2, 3, 3, 3, 2].uniq(); // [1, 2, 3]
Object.clone

オブジェクトのクローンを作る

Object.clone(object);

現状は svn からソース持ってきて、新しい機能を使うことができます。

$ svn co http://dev.rubyonrails.org/svn/rails/spinoffs/prototype prototype
$ cd prototype
$ rake