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?
$("#someid").innerHTML.$('#someid').innerHTMLis a syntax error).$('#someid').innerHTML" No, it can't.innerHTMLthere would also beundefined. jQuery objects are wrappers around sets of elements. You could do$('#someid')[0].innerHTML(note the accessor).Proxy, which isn't available in most browsers yet since it's part of the next ECMAScript standard, which isn't finalized.