获取当前类的声明内容
我正在编写一个类来操作一些innerHTML & div 的 onclick 标签,其中放置了一些函数来调用类中的某些内容,但是,因为在单个页面上将多次使用该类。
我想知道是否有可能在课堂上弄清楚那个特定物体被贴上的标签是什么?
function Bob() {
this.test = function()
{
alert('test');
}
this.Bob = function()
{
element.onClick = function() {(some piece of code).test();};
element.innerHTML = "<a href=\"" + (some piece of code) + ".test();\">Test</a>";
}
}
I am writing a class which manipulates some innerHTML & onclick tags of a div, which puts some function in it that is calls something within the class, however, as there are going to be more than one use of the class on a single page.
I was wondering if it was possible within the class to workout what that particular object had been labelled?
function Bob() {
this.test = function()
{
alert('test');
}
this.Bob = function()
{
element.onClick = function() {(some piece of code).test();};
element.innerHTML = "<a href=\"" + (some piece of code) + ".test();\">Test</a>";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想使用
.bind
将函数绑定到 `thisContextYou want to use
.bind
to bind functions to a `thisContext由于闭包的工作方式,即使 Bob 构造函数返回后,“self”变量仍将在该元素的 onclick 范围内。
只需让不同的类在测试函数内返回不同的警报消息
Due to how closures work the "self" variable will still be inscope within that element's onclick even after the Bob constructor returns.
Simply have different classes return different alert messages inside the test function