0

I'm trying to create a jQuery style object class.

function obj(id){
    if (this.__proto__.constructor !== obj) {
        return new obj(id);
    }
    this.element = document.getElementById(id);

    this.remove = function(){
        this.element.parentNode.removeChild(this.element);
    }
    this.offset = function(){
        return this.element.getBoundingClientRect();
    }
}
obj(id).offset() // defined
obj(id).removeChild() //undefined
obj(id).appendChild() // undefined
obj(id).remove() // undefined

I got a problem. jQuery object can also use as a Javascript DOM object like $('#someid').innerHTML, but my object. I'm thinking about a solution that dynamically checks if a method does not exist in this object class, then return a DOM object return this.element.

How could I do this? Or any better ideas?

4
  • Not sure what you mean by "jQuery style object class", but a jQuery object can't be used as a DOM object like $("#someid").innerHTML. Commented May 24, 2014 at 7:53
  • You're mistaken: jQuery cannot chain DOM methods to jQuery objects ($('#someid').innerHTML is a syntax error). Commented May 24, 2014 at 7:53
  • "jQuery object can also use as a Javascript DOM object like $('#someid').innerHTML" No, it can't. innerHTML there would also be undefined. jQuery objects are wrappers around sets of elements. You could do $('#someid')[0].innerHTML (note the accessor). Commented May 24, 2014 at 7:54
  • 2
    The only way AFAIK to accomplish what you describe would be to use Proxy, which isn't available in most browsers yet since it's part of the next ECMAScript standard, which isn't finalized. Commented May 24, 2014 at 7:54

1 Answer 1

1

jQuery object can also use as a Javascript DOM object like $('#someid').innerHTML

No, it can't. innerHTML there would also be undefined. jQuery objects are wrappers around sets of elements. You could do $('#someid')[0].innerHTML (note the accessor, [0]).

...checks if a method does not exist in this object class...

If you want to test for the existence of a function, you can use typeof:

if (typeof obj.method === "function")

Note that for certain host-provided functions on some older browsers, you may get "object" instead of "function", so you have to allow for that.

But you'd have to do that where you're using the function.

I'm thinking about a solution that dynamically checks if a method does not exist in this object class, then return a DOM object return this.element.

This is a bad idea. Instead, provide a means of accessing the underlying element (the way jQuery does). It will be possible with ES6's proxies, but those aren't available widely yet.

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

Comments

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.