我喜欢它,如果我可以创建自己的类来扩展
HTML元素类,例如
HTMLDivElement或HTML
ImageElement.
假设标准继承模式:
// class A: function ClassA(foo) { this.foo = foo; } ClassA.prototype.toString = function() { return "My foo is " + this.foo; }; // class B: function ClassB(foo,bar) { ClassA.call(this,foo); this.bar = bar; } ClassB.prototype = new ClassA(); ClassB.prototype.toString = function() { return "My foo/bar is " + this.foo + "/" + this.bar; };
function Image2() { // this doesn't work: Image2.prototype.constructor.call(this); // neither does this (only applies to <img>,no equivalent for <span> etc.): Image.call(this); } Image2.prototype = new Image(); // or document.createElement("img"); Image2.prototype.toString = function() { return "My src is " + this.src; };
这是不可能的吗?还是有某种方法?谢谢. =)