使用元素选择器向对象添加功能
Add function to Object with an element selector
我有一个对象
function Object(name, button) {
this.name = name;
this.button = button;
var alert = function () {
alert("x");
};
}
var ExampleOne = new Object('Example One', $('.exampleButton'));
如何让对象内部的函数在 Object[button] 事件发生时触发...
使用-
ExampleOne.button.click(function () {
ExampleOne.alert();
});
无效。
它对我有用。再次检查您的代码。也许你忘了包括 jQuery?
ExampleOne.button.click(function(e) {
alert('success!');
});
var alert = function
是局部函数,无法在该对象外部访问。如果您希望新实例可以访问它,请使用 this
:
声明它
function Object(name, button) {
this.name = name;
this.button = button;
this.alert = function () {
alert("x");
};
}
Fiddle: http://jsfiddle.net/qow3qu0m/2/
我有一个对象
function Object(name, button) {
this.name = name;
this.button = button;
var alert = function () {
alert("x");
};
}
var ExampleOne = new Object('Example One', $('.exampleButton'));
如何让对象内部的函数在 Object[button] 事件发生时触发...
使用-
ExampleOne.button.click(function () {
ExampleOne.alert();
});
无效。
它对我有用。再次检查您的代码。也许你忘了包括 jQuery?
ExampleOne.button.click(function(e) {
alert('success!');
});
var alert = function
是局部函数,无法在该对象外部访问。如果您希望新实例可以访问它,请使用 this
:
function Object(name, button) {
this.name = name;
this.button = button;
this.alert = function () {
alert("x");
};
}
Fiddle: http://jsfiddle.net/qow3qu0m/2/