3

I need to manipulate HTML tags through a class as such:

/*
 * Begin: HTML Class
 */
HTML = function(el, property) { // Construct
    this.el = el;
    this.property = new Array;
    var HTML = document.createElement(el);

    this.element = function() {
        return HTML;
    };

    HTML.objects.push(this);

    if (typeof property == "object")
        for (i in property)
            this.addProperty(i, property[i]);
};

HTML.objects = new Array; // Registers all new HTML objects.

// Adds a new property to HTML current element.
HTML.prototype.addProperty = function(name, value) {
    this.property[name] = value;
    this.getHTML()[name] = value;
};

// Retrieves current HTML element.
HTML.prototype.getHTML = function() {
    return this.element();
};

// Clones current HTML objects with same construct arguments.
HTML.prototype.clone = function() {
    return new HTML(this.el, this.property);
};
/*
 * End: HTML Class
 */

Each time new HTML(...) is called, newly created instance must be stored within HTML.objects which is a static property of HTML whose role is to keep track of all HTML objects. But now when it reaches to HTML.objects.push(this); it will return undefined property error. After, I tried to call for HTML.objects in firebug and it was definitely defined. As function(...) { ... } is called at instantiation, shouldn't it be able to access HTML.objects property?
Thanks.

2
  • I would strongly recommend to resolve the name clash between HTML and HTML. Commented May 31, 2015 at 10:40
  • Also HTML is a particularly bad name for a DOM element (which has nothing to do with HTML markup). Some goes for the .getHTML() method - just use the .element() one. Commented May 31, 2015 at 10:41

1 Answer 1

2

The proprety HTML.objects doesn't exists because in your constructor's scope HTML is the variable you have defined here:

var HTML = document.createElement(el);

so, when you make this call:

HTML.objects.push(this);

you are trying to access the proprey of the just declared HTML variable.

In fact, if you try to replace the first line with:

var HTML_INNER = document.createElement(el);

It'll work

Sign up to request clarification or add additional context in comments.

2 Comments

WOW ! Now I feel shameful. I was testing it with another object which was working fine. But in the other object I was not defining an internal var with same name as object. Thanks a lot.
:) another advice is: you use the same name 'property' for a constructor parameter and a object field, but you don't assign the parameter to the field in the constructor. This could lead to misunderstandings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.