Home > Web Front-end > JS Tutorial > body text

Prototype learning tool function learning ($w, $F method)_prototype

WBOY
Release: 2016-05-16 18:50:04
Original
1199 people have browsed it
$w method
Splits a string into an Array, treating all whitespace as delimiters. Equivalent to Ruby's %w{foo bar} or Perl's qw(foo bar).
Copy code The code is as follows:

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

This method is used The whitespace character splits the string into an array and returns it.
Example:
Copy code The code is as follows:

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

$F method
Returns the value of a form control. This is a convenience alias of Form .Element.getValue.
Copy code The code is as follows:

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;
},
//Omitted, we will discuss this object in detail later Description
......
//====> Object.isUndefined
function isUndefined(object) {
return typeof object === "undefined";
}

This function finally returns the value of the passed in parameter. It can be seen from the methods defined in the Form.Element.Serializers object that the $F method obtains the value of the Form element. If you define a div and then call this method, Form.Element.Serializers[method] is not a function exception, if the given ID does not exist, an element has no properties exception will be thrown.
In the method in Form.Element.Serializers, first check whether the value parameter exists. If it exists, it is equivalent to assigning a value to the element parameter. If it does not exist, the value of the element will be returned.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!