Home > Web Front-end > JS Tutorial > Prototype learning tool function learning($method)_prototype

Prototype learning tool function learning($method)_prototype

WBOY
Release: 2016-05-16 18:50:07
Original
1089 people have browsed it

$
$$
$A
$F
$H
$R
$w
Try.these
document.getElementsByClassName
$ method—— Known as the Swiss Army knife
If provided with a string, returns the element in the document with matching ID; otherwise returns the passed element. Takes in an arbitrary number of arguments. All elements returned by the function are extended with Prototype DOM extensions.

Copy code The code is as follows:

function $(element) {
if (arguments.length > 1) {
for (var i = 0, elements = [], length = arguments.length; i < length;
i )
elements.push( $(arguments[i]));
return elements;
}
if (Object.isString(element))
element = document.getElementById(element);
return Element.extend (element);
}

First check the length of the parameter passed in:

If the length is equal to 1, then determine whether the parameter passed in is String. If it is String, call the getElementById method to obtain the corresponding object, and finally let the returned object inherit all methods of Element, so that the returned object can directly call various methods defined in the Element object. For example,
Copy code The code is as follows:

// Note quite OOP-like...
Element.hide('itemId');
// A cleaner feel, thanks to guaranted extension
$('itemId').hide();

If the length is greater than 1, then recursively call the $ method (elements.push($(arguments[i]));), which means that the parameters passed in can be multi-dimensional arrays:
$(['A','B',['C ','D',['E','F']]]), of course, the returned object array is also returned.
If the length is equal to 0, undefined is returned, that is, alert($()) is called directly.
Look at the Object.isString method in detail:
Copy code The code is as follows:

function isString(object) {
return getClass(object) === "String";
}

//=====> getClass()

function getClass(object) {
return Object.prototype.toString.call(object)
.match(/^[objects(. *)]$/)[1];
}

Mainly determines what type of object is returned through the internal method getClass of the Object object. The toString method of Object is called in getClass. , and then use regular expressions to extract the string representing the specific object

Object.prototype.toString.call("2222") Return "[object String]" Get "String"

Object. prototype.toString.call(2222) Returns "[object Number]" Get "Number"



Object.prototype.toString.call(/^$/) Returns "[object RegExp] " Get "RegExp"

Why should we use Object's toString method here? Because if we call "2222".toString() directly, "2222" will be returned. That is to say, the object inherited from Object will be reused. The toStirng method is written, so the toString of Object needs to be called here.
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