JS Analog High-level language overload

Source: Internet
Author: User

JS simulates the overloading of high-level languages recursively, and I add element nodes as an example:

//insert a new child element before the existing child elementvarBefore =function(elem,newelement,targetelement) {if(targetelement = = =Undefined)returnBefore (newelement? newelement.parentnode:NULL, elem,newelement); //compatible with IE7 the following target node is empty error problem    if(!Targetelement)returnelem? Elem.appendchild (newelement):NULL; returnElem.insertbefore (newelement,targetelement);};//inserts a new child element after the existing child elementvarafter =function(elem,newelement,targetelement) {if(targetelement = = =Undefined) {        returnAfter (newelement? newelement.parentnode:NULL, elem,newelement); }        return!elem | | (elem.lastelementchild | | elem.lastchild) = = targetelement?//if the last node is the target element, it is added directly. Because the default is the lastBefore (elem,newelement,NULL)    :        //if it is not, it is inserted in front of the next sibling node of the target Element. Which is behind the target Element.Before (newelement, targetelement.nextelementsibling | |targetelement.nextsibling);};//Call:varElem = getElementById (' D1 ')), newelement= document.createelement (' Div '), targetelement= Elem.firstelementchild | |Elem.firstchild;before (elem,newelement,targetelement); before (newelement,targetelement); after (elem, newelement,targetelement); after (newelement,targetelement);

JS Analog High-level language overload

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.