创建在对象上调用appendchild时的默认行为

发布于 2024-12-29 04:33:05 字数 1073 浏览 0 评论 0原文

我正在为一些 HTML 元素创建包装类,我想知道是否有一种方法可以在调用 .appendChild 时为我的类指定默认行为。

// Very simple textbox wrapper class
function MyWrapperClass(){
    this.input = document.createElement('input'); // textbox
}
MyWrapperClass.prototype.setValue = function(v){
    this.input.value = v;
}

// Add instance of my wrapper class to DOM
var foo = new MyWrapperClass();
document.body.appendChild( foo.input ); // Works fine.

这已经足够好了。但我试图将我的代码抽象到足以实现这一点:

// Add instance of my wrapper class to DOM
var foo = new MyWrapperClass();
document.body.appendChild( foo );

当对 foo 调用appendChild 时,会自动返回 foo.input 。

现在,我意识到我可以修改我的包装类以返回其构造函数中的输入元素,但是当我这样做时,我失去了调用任何类方法的能力:

// Modified wrapper, returns input on instancing
function MyWrapperClass(){
   this.input = document.createElement('input'); // textbox
   return this.input
}

var foo = new MyWrapperClass();
foo.setValue('Hello'); // ERROR: html input element has no setValue method

那么有什么方法可以覆盖默认行为对象并在 foo 上调用 .appendChild 时返回 foo.input?

I'm working on creating wrapper classes for some HTML elements and I'd like to know if there is a way to specify default behavior for my class when .appendChild is called on it.

// Very simple textbox wrapper class
function MyWrapperClass(){
    this.input = document.createElement('input'); // textbox
}
MyWrapperClass.prototype.setValue = function(v){
    this.input.value = v;
}

// Add instance of my wrapper class to DOM
var foo = new MyWrapperClass();
document.body.appendChild( foo.input ); // Works fine.

That works well enough. But I'm trying to abstract my code enough to get to this:

// Add instance of my wrapper class to DOM
var foo = new MyWrapperClass();
document.body.appendChild( foo );

where foo.input is automatically returned when appendChild is called on foo.

Now, I realize that I can modify my wrapper class to return the input element in it's constructing function, but when I do that, I lose the ability to call any of the class methods:

// Modified wrapper, returns input on instancing
function MyWrapperClass(){
   this.input = document.createElement('input'); // textbox
   return this.input
}

var foo = new MyWrapperClass();
foo.setValue('Hello'); // ERROR: html input element has no setValue method

So is there any way to override the default behavior of the object and return foo.input when .appendChild is called on foo?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2025-01-05 04:33:05

如果您为对象创建一个方法 append() ,您将能够比执行 document.body.appendChild( foo ); 进行更多抽象,

function MyWrapperClass(){
   this.input = document.createElement('input'); // textbox
   return this;
}
MyWrapperClass.prototype.setValue = function(v){
    this.input.value = v;
}
MyWrapperClass.prototype.append = function(){
    document.body.appendChild(this.input)
}


var foo = new MyWrapperClass();
foo.setValue('test');
foo.append();

请参阅此小提琴:http://jsfiddle.net/yJ44E/
注意:您还可以更改 append() 方法,以便接受节点作为要在其中追加元素的参数。

if you create a method append() of your object you would be able to abstract more than doing document.body.appendChild( foo );

function MyWrapperClass(){
   this.input = document.createElement('input'); // textbox
   return this;
}
MyWrapperClass.prototype.setValue = function(v){
    this.input.value = v;
}
MyWrapperClass.prototype.append = function(){
    document.body.appendChild(this.input)
}


var foo = new MyWrapperClass();
foo.setValue('test');
foo.append();

see this fiddle: http://jsfiddle.net/yJ44E/
Note: you could also change the append() method so to accept the node as an argument in which you want to append the element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文