Introduction to Prototype series (2) prototype. js Methods

Source: Internet
Author: User

Var Class = {

Create: function (){

Return function (){

This. initialize. apply (this, arguments );

}

}

};

Object. extend = function (destination, source ){

For (var property in source) destination [property] = source [property];

Return destination;

};

Function. prototype. bind = function (object ){

Var _ method = this;

Return function (){

// Replace this in _ method with the object and call the _ method

Return _ method. apply (object, arguments );

}

};

// ForEach method: change this to a bind object, and call the fn method with the parameter this [I]. this [I] is the current array element, and I is the current index.

If (! Array. prototype. forEach ){

Array. prototype. forEach = function (fn, bind ){

For (var I = 0; I <this. length; I ++) fn. call (bind, this [I], I );

};

}

// The each method is the same as forEach.

Array. prototype. each = Array. prototype. forEach;

// The method for returning the string camel. e.g. "font-color". camelize ()-> fontColor, Which is required when processing css attributes

String. prototype. camelize = function (){

Return this. replace (/-\ D/gi, function (match ){

Return match. charAt (match. length-1). toUpperCase ();

});

};

Var $ A = function (iterable ){

Var nArray = [];

For (var I = 0; I <iterable. length; I ++) nArray. push (iterable [I]);

Return nArray;

};

/*

* If the input parameter is one, an element is returned. Otherwise, an array is returned.

**/

Function $ (){

If (arguments. length = 1) return get $ (arguments [0]);

Var elements = [];

$ C (arguments). each (function (el ){

Elements. push (get $ (el ));

});

Return elements;

Function get $ (el ){

If (typeof el = 'string') el = document. getElementById (el );

Return el;

}

};

If (! Window. Element) var Element = {};

Object. extend (Element ,{

Remove: function (element) {// delete a specified element

Element = $ (element );

Element. parentNode. removeChild (element );

},

HasClassName: function (element, className) {// checks whether the specified element has a corresponding css class.

Element = $ (element );

Return !! Element. className. match (new RegExp ("\ B" + className + "\ B "));

},

AddClassName: function (element, className) {// Add the specified css to the corresponding element

Element = $ (element );

If (! Element. hasClassName (element, className) element. className = (element. className + ''+ className );

},

RemoveClassName: function (element, className) {// Delete the specified css from the corresponding element.

Element = $ (element );

If (Element. hasClassName (element, className) element. className = element. className. replace (className ,'');

}

});

// Return elements with the same css class.

Document. getElementsByClassName = function (className ){

Var elements = [];

Var all = document. getElementsByTagName ('*');

$ A (all). each (function (el ){

If (Element. hasClassName (el, className) elements. push (el );

});

Return elements;

};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.