Prototype 學習 工具函數學習($w,$F方法)_prototype

來源:互聯網
上載者:User
$w方法
Splits a string into an Array, treating all whitespace as delimiters. Equivalent to Ruby's %w{foo bar} or Perl's qw(foo bar).
複製代碼 代碼如下:

function $w(string) {
if (!Object.isString(string)) return [];
string = string.strip();
return string ? string.split(/\s+/) : [];
}

這個方法就是用空白字元把字串分成數組,然後返回。
例子:
複製代碼 代碼如下:

$w('apples bananas kiwis') // -> ['apples', 'bananas', 'kiwis']

$F方法
Returns the value of a form control. This is a convenience alias of Form.Element.getValue.
複製代碼 代碼如下:

var $F = Form.Element.Methods.getValue;
//====>getValue()
getValue: function(element) {
element = $(element);
var method = element.tagName.toLowerCase();
return Form.Element.Serializers[method](element);
}
//====>Serializers
Form.Element.Serializers = {
input: function(element, value) {
switch (element.type.toLowerCase()) {
case 'checkbox':
case 'radio':
return Form.Element.Serializers.inputSelector(element, value);
default:
return Form.Element.Serializers.textarea(element, value);
}
},
inputSelector: function(element, value) {
if (Object.isUndefined(value)) return element.checked ? element.value :
null;
else element.checked = !!value;
},
textarea: function(element, value) {
if (Object.isUndefined(value)) return element.value;
else element.value = value;
},
//省略,以後說到這個對象的時候在詳細說明
......
//====> Object.isUndefined
function isUndefined(object) {
return typeof object === "undefined";
}

這個函數最後就是返回傳入參數的值。從Form.Element.Serializers 這個對象裡面定義的方法可以看出,$F方法取得的是Form元素的值,如果定義一個div然後調用這個方法將會拋出Form.Element.Serializers[method] is not a function異常,如果給定的ID不存在將會拋出element has no properties異常。
在Form.Element.Serializers 裡面的方法中先檢查value這個參數是否存在,存在就相當於給element參數賦值,不存在就會返回element的值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.