Hi, I'm having trouble extending instances of ActiveXObject using
Object.extend in IE.
I'm getting the message: "Object doesn't support this property or
method"
Here's my sample code, to show working and non-working examples:
var newDef = {
foo : function(){
return "hey there";
}
};
alert("starting");
var string;
var doc;
var obj;
try{
if(typeof ActiveXObject == "undefined"){
doc = document.implementation.createDocument("", "", null);
}else{
doc = new ActiveXObject("Microsoft.XMLDOM");
}
Object.extend(doc, newDef);
alert("1 object Extended");
// FF: this message is displayed
}catch(e){
alert("2 " + e.description);
// IE : this message is displayed.
}
if(typeof ActiveXObject != "undefined"){
try{
Object.extend(ActiveXObject.prototype, newDef);
alert("3 extended ActiveXObject.prototype");
// IE : this message is displayed
doc = new ActiveXObject("Microsoft.XMLDOM");
try{
var text = doc.foo();
alert("4 " + text);
}catch(e){
alert("5 " + e.description);
// IE : this message is displayed
}
}catch(e){
alert("6 " + e.description);
}
}
try{
obj = new Object();
Object.extend(obj, newDef);
alert("7 extended string");
// IE & FF : this message is displayed.
}catch(e){
alert("8 " + e.description);
}
try{
alert("9 " + obj.foo());
// IE & FF: this message is displayed.
}catch(e){
alert("10 " + e.description);
}
try{
string = "hey";
Object.extend(String.prototype, newDef);
alert("11 " + string.foo());
// IE & FF : this message is displayed.
}catch(e){
alert("12 " + e.description);
}
the problem areas for me are messages 2 and 5. Is there a reason I
can't extend the ActiveXObject instance, or if, when I extend the
prototype, I can't actually use the methods defined?
I'm able to extend the document instance firefox creates, and I was
really hoping to use the same approach in IE. Is there a workaround?
Sorry if maybe I've posted this twice...
on 15.05.2008 23:05
on 16.05.2008 06:23
Looks like you can't extend ActiveXObject.
Have you considered using a "wrapper" or "factory" approach?
function createDoc(methods) {
return Object.extend(new ActiveXObject("Microsoft.XMLDOM"),
methods);
}
- kangax
on 16.05.2008 16:53
Sorry, I don't see how that's any different, since in what you
describe below, I'm still calling Object.extend on an instance of
ActiveXObject.
I tried it, and got the same error.
Also, I can't extend instances of nodes on that document:
var result = xPath(xsdDoc, "/xs:schema/xs:element");
var node = result[0];
alert("node is " + node.nodeName); // to show that it's a valid node
try{
Object.extend(node, exXsdElem);
}catch(e){
alert("error 1a" + e.description);
// in IE this error appears
}
I can get around the issue of not extending the document itself; it's
not crucial to my design.
But, being able to extend instances of NODES is. It's very convenient
to be able to attach different methods and variables to an xml dom
element, depending on the name of the node.
Is this really not possible in IE?