If I assign
displayUL = new displayListMaker({{ $displays }});
How do I get the variable displayUL if this is even possible
var displayListMaker = function(displays){
like
this.self = wantthevariable
so I can generate html that calls itself
return '<a onclick="'+this.self+'.callAFunc(withdata)">call myself :)</a>'
You can't get the name of the variable that will ultimately reference an object inside the constructor because there could eventually be multiple references to the same object, so how could it know which to use?
If this is for an inline handler attribute, then having the name of the local variable won't matter anyway since the handler body is executed in the global variable scope.
If the displayUL
variable is global, one solution could be to pass the name of the global to the constructor, and have it add the global variable there, and code the name into the string.
new displayListMaker($displays, "displayUL");
var displayListMaker = function(displays, name) {
window[name] = displays;
'<a onclick="' + name + '.callAFunc(withdata)">call myself :)</a>'
// ...and so on
Though I'd venture a guess that your issue is entire around your onclick="..."
attribute. Creating the DOM element directly and using an elem.onclick = func...
property, while keeping a closure reference to the object is probably what you really need.
new displayListMaker($displays);
var displayListMaker = function(displays, name) {
var that = this;
var elem = document.createElement("a")
elem.innerHTML = "call myself :)"
elem.onclick = function() {
that.callAFunc(withdata);
}
// ...and so on
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.