Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9264139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:45:08+00:00 2026-06-18T13:45:08+00:00

This project is just for my own satisfaction, so I’m not really looking for

  • 0

This project is just for my own satisfaction, so I’m not really looking for other libraries to accomplish this. I’m trying to make an HTML element library:

var ElementLibrary = function() {};
(function() {
    var _concatArray = [,'Element'];
    var _varArray = [
        'html',
        'head', 'title', 'base', 'link', 'meta', 'style',
        'script',
        'body', 'section', 'nav', 'article', 'aside', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hgroup', 'header', 'footer', 'address', 'main',
        'p', 'hr', 'pre', 'blockquote', 'ol', 'ul', 'li', 'dl', 'dt', 'dd', 'figure', 'figcaption', 'div',
        'a', 'em', 'strong', 'small', 's', 'cite', 'q', 'dfn', 'abbr', 'data', 'time', 'code', 'var', 'samp', 'kbd', 'sub', 'sup', 'i', 'b', 'u', 'mark', 'ruby', 'rt', 'rp', 'bdi', 'bdo', 'span', 'br', 'wbr',
        'ins', 'del',
        'img', 'iframe', 'embed', 'object', 'param', 'video', 'audio', 'source', 'track', 'canvas', 'map', 'area', 'svg', 'math',
        'table', 'caption', 'colgroup', 'col', 'tbody', 'thead', 'tfoot', 'tr', 'td', 'th',
        'form', 'fieldset', 'legend', 'label', 'input', 'button', 'select', 'datalist', 'optgroup', 'option', 'textarea', 'keygen', 'output', 'progress', 'meter',
        'details', 'summary', 'command', 'menu'
    ];
    var _l = _varArray.length;
    for (var i = 0; i < _l; i++) {
        _concatArray[0] = _varArray[i];
        ElementLibrary.prototype[_concatArray.join('')] = document.createElement(_varArray[i]);
        ElementLibrary.prototype[_varArray[i]] = function() {
            return this[_concatArray.join('')].cloneNode(false);
        };
    }
})();

The way I had originally begun to write it would have ended up at over 400 lines, so I wanted to do it in a more compact way. The good news, it partially works. If I do:

var el = new ElementLibrary();
el.pElement // <---that is correctly a paragraph element

However, that’s not really the intended use. I want to get a shallow clone off of the single prototypal property like:

var el = new ElementLibrary();
el.p() // <---this is where I run into trouble

If I call any of my ElementLibrary functions, it ONLY returns the last element in my array (menu in this case). Why does every function return only the last element when the properties are all declared properly?

EDIT: I added a fiddle and pulled the _concatArray business out.

Solution:

var ElementLibrary = function() {};
(function() {
    var _varArray = [
        'html',
        'head', 'title', 'base', 'link', 'meta', 'style',
        'script',
        'body', 'section', 'nav', 'article', 'aside', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hgroup', 'header', 'footer', 'address', 'main',
        'p', 'hr', 'pre', 'blockquote', 'ol', 'ul', 'li', 'dl', 'dt', 'dd', 'figure', 'figcaption', 'div',
        'a', 'em', 'strong', 'small', 's', 'cite', 'q', 'dfn', 'abbr', 'data', 'time', 'code', 'var', 'samp', 'kbd', 'sub', 'sup', 'i', 'b', 'u', 'mark', 'ruby', 'rt', 'rp', 'bdi', 'bdo', 'span', 'br', 'wbr',
        'ins', 'del',
        'img', 'iframe', 'embed', 'object', 'param', 'video', 'audio', 'source', 'track', 'canvas', 'map', 'area', 'svg', 'math',
        'table', 'caption', 'colgroup', 'col', 'tbody', 'thead', 'tfoot', 'tr', 'td', 'th',
        'form', 'fieldset', 'legend', 'label', 'input', 'button', 'select', 'datalist', 'optgroup', 'option', 'textarea', 'keygen', 'output', 'progress', 'meter',
        'details', 'summary', 'command', 'menu'
    ];
    var _l = _varArray.length;
    for (var i = 0; i < _l; i++) {
        var _temp = _varArray[i];
        var _tempElement = _temp + 'Element';
        (function(t, te) {
            ElementLibrary.prototype[te] = document.createElement(t);
            ElementLibrary.prototype[t] = function() {
                return this[te].cloneNode(false);
            };
        })(_temp, _tempElement);
    }
})();

var el = new ElementLibrary();
alert(el.p());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T13:45:09+00:00Added an answer on June 18, 2026 at 1:45 pm

    The functions you create in that loop do not refer to “frozen” versions of “_concatArray”. They all share a reference to the very same array.

    Wrap them in another function:

    ElementLibrary.prototype[_varArray[i]] = (function(ca) {
      return function() {
        return this[ca.join('')].cloneNode(false);
      };
    })(_concatArray);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set up a test project looking like my own project just
I all, I just follow this great example: http://qt-project.org/doc/qt-4.8/tools-customcompleter.html I copy the code exactly
This might not have a major usecase in projects, but I was just trying
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
I just found this project which provides a way of running Google App Engine
This is a MVC 3 project. Just for testing, I have public class MyRoleProvider
Just recently I dove into the VideoLAN open source project. This was my first
Am using Mediainfo library in my C# project,before start invoking this dll,i just ran
Imagine a very big gwt project of application. This is just a some form
Just a quick one, hopefully. I've coded up this: <script> $(div.design-project).css('display', 'none'); function InsertContent(tid)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.